Save a File on External Storage
2015-11-12 09:10
260 查看
Save a File on External Storage
/* 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; }
Although the external storage is modifiable by the user and other apps, there are two categories of files you might save here:
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.
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.
If you want to save public files on the external storage, use the getExternalStoragePublicDirectory() method to get a File representing the appropriate directory on the external storage. The method takes an argument specifying the type of file you want to save so that they can be logically organized with other public files, such as DIRECTORY_MUSIC or DIRECTORY_PICTURES. For example:
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; }
If you want to save files that are private to your app, you can acquire the appropriate directory by calling getExternalFilesDir() and passing it a name indicating the type of directory you’d like. Each directory created this way is added to a parent directory that encapsulates all your app’s external storage files, which the system deletes when the user uninstalls your app.
For example, here’s a method you can use to create a directory for an individual photo album: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; }
相关文章推荐
- IIS 6文件解析漏洞
- WebViewJavascriptBridge-Obj-C和JavaScript互通消息的桥梁
- c语言的头文件-不是c++类的头文件?
- 【Unity实用小方法】随机生成不重复的数字
- AD DS功能浅析--AD DS概述
- Linux下如何查看tomcat是否启动/系统日志等
- JNI:JAVA调用C库方法
- TCP/IP
- 新版QQ登录Canvas闪动特效
- Android WebView打开https地址错误的问题
- maven打jar例子
- 黑马程序员——C语言基础08—结构体/枚举
- javascript深入理解js闭包
- Switch用String做参数
- JavaScript中的运算操作陷阱
- 实验二 作业调度模拟实验
- Delphi操作excel说明
- 从字符串中提取逐一提取数字id
- Python操作Redis之有序集合
- c++ 字符串与int,long ,double间的转换