1. 存储
存储的四种类型
Shared Preference
内部存储
外部存储
SQLite DB
网络链接
1.1 SharedPreferences
用来存储和恢复持久数据,适用于少量的、k-value类型的数据。
SharedPreference对象 被系统框架管理,可以私有或者共享。注意和Preference区别开来,Preference用来管理系统设置的UI
编程的三个步骤
1\创建/获取对象句柄
getSharedPreferences() 当在Context里面需要多个不同共享的perference文件时候使用。getPreferences() 当一个Activity中只有一个sharedprefence文件时候,获取一个默认的文件。
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
getString(R.string.preference_file_key),Context.MODE_PRIVATE);
with MODE_WORLD_READABLE
orMODE_WORLD_WRITEABLE
, 设置其他程序可以访问
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
2、存数据
获取一个SharedPreferences.Editor 对象
SharedPreferences.Editor edit()
然后使用putInt putString 等存储数据
putBoolean
最后commit
commit()
3、取数据
long highScore= sharedPref.getInt("newHightScore",(int) defValue);
还可以设计一个默认存储值。
1.1 内部存储
File对象用来读取大量的连续数据
选择内部、外部存储
内部存储:一直可用、默认为本APP自有、程序卸载后文件也删除
外部存储:不一定可用、整个手机全局可读、程序卸载后文件保存。自有通过getExternalFilesDir()目录内的文件将删除
1、 获取文件访问权限
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 隐含了拥有读权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
2、 存储文件到内部存储
A、 获取文件目录
File getFilesDir() 获取内部文件目录
getCacheDir() 获取App 临时cache文件的内部目录
确保及时删除不必要的文件、并且请求目录大小合适
B 创建一个文件,使用File构造函数
File file = new File(context.getFilesDir(), filename);
或者如下:
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
或者如果存储Cache文件
public File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
catch (IOException e) {
// Error while creating file
}
return file;
}
-----------------------------------
FileOutputStream ss = openFileOutput(file, mode)
write()
close(0
MODE_APPEND MODE_WORLD_EADABLE MODE_WORLD_WRITEABLE
----
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos =openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
------
存储临时cache文件
getCacheDir()
其它
getFilesDir()
getDir()
deleteFile()
fileList()
--------------------------
1.2 外部存储
获取外部存储状态getExternalStorageState()
当状态等于 MEADIA_MOUNTED时,文件可以读写
1、检查外部存储状态
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state =Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but allwe need
// to know is we can neither readnor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
3、 外部文件类型:
公共文件:其它程序可以访问的文件,当程序卸载后,这些文件不删除
私有文件:程序卸载后,文件被删除
2、存储外部公共文件目录:
getExternalStoragePublicDirectory(DIRECTORY_MUSIC, ) 等
public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
4、 获取外部私有文件目录getExternalFilesDir()
传递null参数,返回应用程序的顶层目录、
public File getAlbumStorageDir(Context context, String albumName) {
// Get the directory for the app's private pictures directory.
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}