以文件的形式保存数据适合从文件开头读或者写到文件结束。例如将图片保存在文件中.
所有的Android 设定都分为内存和外存 路径来保存文件.
我们先看看如果在内存中保存文件
你可以通过下面两个函数来得到要保存文件的路径.
getFilesDir():来获取保存文件的路径.这个函数返回file 对象是保存在你app 内部的。
getCacheDir():文件保存在app的临时cache文件,当不需要火车size超过给定的size,需要删掉。当系统的memory不足时,保存在getCacheDir()路径下的文件会被删掉.
得到路径后要怎么产生新的文件呢?
File file = new File(context.getFilesDir(), filename);
你可以调用openFileOutput() 来得到一个FileOutPutStream,然后写文件到内部存储空间,下面是一个实际使用的例子.
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();
}
你如果需要缓存一些文件,可以通过下面的方式。
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;
}
如果在外存(例如SD卡)中保存文件.首先有在manifest文件中声明读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
或者
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
要判断当前外存(SD)卡是否处于mount状态。
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
如果你想把文件保存在外存的公共目录下,保存在公共目录下的是文件可以被其他app访问,即使你的app 被删掉,文件还是会得以保存,如下所示:
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;
}
如果你保存的文件,只想被你的app 访问到,可以采用下面的方法.
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;
}
你可以通过gerFreeSpace或者getTotalSpace来得到free或者总的memory值,这样就可以控制你的文件的size,举例如下:
final long freeBytes = path.getFreeSpace();
71 final long totalBytes = path.getTotalSpace();
72 final long usedBytes = totalBytes - freeBytes;
73
74 final String used = Formatter.formatFileSize(context, usedBytes);
75 final String total = Formatter.formatFileSize(context, totalBytes);
76 setSummary(context.getString(R.string.storage_volume_summary, used, total));
77 mUsedPercent = (int) ((usedBytes * 100) / totalBytes);
可以通过下面两种方式删掉文件.
myFile.delete();
mycontext.deleteFile(fileName)
所有的Android 设定都分为内存和外存 路径来保存文件.
我们先看看如果在内存中保存文件
你可以通过下面两个函数来得到要保存文件的路径.
getFilesDir():来获取保存文件的路径.这个函数返回file 对象是保存在你app 内部的。
getCacheDir():文件保存在app的临时cache文件,当不需要火车size超过给定的size,需要删掉。当系统的memory不足时,保存在getCacheDir()路径下的文件会被删掉.
得到路径后要怎么产生新的文件呢?
File file = new File(context.getFilesDir(), filename);
你可以调用openFileOutput() 来得到一个FileOutPutStream,然后写文件到内部存储空间,下面是一个实际使用的例子.
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();
}
你如果需要缓存一些文件,可以通过下面的方式。
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;
}
如果在外存(例如SD卡)中保存文件.首先有在manifest文件中声明读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
或者
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
要判断当前外存(SD)卡是否处于mount状态。
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
如果你想把文件保存在外存的公共目录下,保存在公共目录下的是文件可以被其他app访问,即使你的app 被删掉,文件还是会得以保存,如下所示:
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;
}
如果你保存的文件,只想被你的app 访问到,可以采用下面的方法.
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;
}
你可以通过gerFreeSpace或者getTotalSpace来得到free或者总的memory值,这样就可以控制你的文件的size,举例如下:
final long freeBytes = path.getFreeSpace();
71 final long totalBytes = path.getTotalSpace();
72 final long usedBytes = totalBytes - freeBytes;
73
74 final String used = Formatter.formatFileSize(context, usedBytes);
75 final String total = Formatter.formatFileSize(context, totalBytes);
76 setSummary(context.getString(R.string.storage_volume_summary, used, total));
77 mUsedPercent = (int) ((usedBytes * 100) / totalBytes);
可以通过下面两种方式删掉文件.
myFile.delete();
mycontext.deleteFile(fileName)