将数据保存在外部存储器上

 /* 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;
}

该问外部存储器上的文件前,需要先做如上的判断,此外,需要在应用中添加用户权限:

<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
<manifest ...>
    <uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/>
    ...
</manifest>

外部存储器与内部存储器的比较:

Internal storage:

  • It's always available.
  • Files saved here are accessible by only your app by default.
  • When the user uninstalls your app, the system removes all your app's files from internal storage.

Internal storage is best when you want to be sure that neither the user nor other apps can access your files.

External storage:

  • It's not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device.
  • It's world-readable, so files saved here may be read outside of your control.
  • When the user uninstalls your app, the system removes your app's files from here only if you save them in the directory from getExternalFilesDir().

External storage is the best place for files that don't require access restrictions and for files that you want to share with other apps or allow the user to access with a computer.

外部存储文件有两类:

public files:

Files that should be freely available to other apps and to the user. When the user uninstalls your app, these files should remain available to the user.

For example, photos captured by your app or other downloaded files.

用户和其它app均可访问,当用户卸载该应用后,该类文件不会被移除,例如照片

private files:

Files that rightfully belong to your app and should be deleted when the user uninstalls your app. Although these files are technically accessible by the user and other apps because they are on the external storage, they are files that realistically don't provide value to the user outside your app. When the user uninstalls your app, the system deletes all files in your app's external private directory.

For example, additional resources downloaded by your app or temporary media files.

该类文件仅属于你的应用,应用卸载后,数据删除。例如,应用下载的资源文件等。

保存文件到内部存储器:

 File file = new File(context.getFilesDir(), filename);

 /*Alternatively, you can call openFileOutput() to get a FileOutputStream that writes to a file in your internal directory. For example, here's how to write some text to a file:*/

 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 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;
}
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;
}

最新文章

  1. ios 类似微信红点显示功能
  2. Android应用开发是否应避免使用枚举?
  3. 让下拉框中同时显示Key与Value
  4. C# 生成二维码,彩色二维码,带有Logo的二维码及普通条形码
  5. chrome调试找不到 XXXX.min.map 原因及解决办法
  6. 升级IOS 9 和 XCode 7 引起的问题
  7. MFC——从实现角度分析微云界面
  8. c# DataTable 中 Select 和 Clone 用法结合
  9. (转)Asp.NetURL重写的一种方法
  10. Kotlin学习第一课:从对比Java开始
  11. Bootstrap中的datetimepicker用法,只看一眼就全懂了
  12. VS系列控制台闪退解决
  13. js 更改对象属性名
  14. Spring Cloud Alibaba Nacos 入门
  15. python---tornado初识(1)
  16. [7] Windows内核情景分析---线程同步
  17. Linux 文本编辑器 vim
  18. Hadoop---目录结构介绍
  19. win8换win7的操作方法
  20. H5页面关于android软键盘弹出顶起底部元素的解决方案

热门文章

  1. LDAP 在ubuntu14.04下的安装配置install and configure
  2. Maven报错:Missing artifact jdk.tools:jdk.tools:jar:1.6
  3. COGS 678. 双重回文数
  4. I2C总线协议学习笔记 (转载)
  5. UI5 Source code map机制的细节介绍
  6. CRUD全栈式编程架构之导入导出的设计
  7. DOM笔记(十一):JavaScript对象的基本认识和创建
  8. window/win7/wamp下安装Xdebug
  9. numpy中生成随机矩阵并打印出矩阵的shape
  10. Spring/Spring boot中静态变量赋值