Android uses a file system that’s similar to disk-based file systems on other platforms. This lesson describes how to work with the Android file system to read and write files with the File APIs.
A File object is suited to reading or writing large amounts of data in start-to-finish order without skipping around. For example, it’s good for image files or anything exchanged over a network.
This lesson shows how to perform basic file-related tasks in your app. The lesson assumes that you are familiar with the basics of the Linux file system and the standard file input/output APIs in java.io.
安卓平台使用了一种类似于基于磁盘的文件系统。用文件APIs可以读写文件。一个File对象适合于从开始到结尾读写daliang的数据,而不需要跳过周围的数据。这样做对于图片文件或通过网络传输的文件很合适。
All Android devices have two file storage areas: “internal” and “external” storage. These names come from the early days of Android, when most devices offered built-in non-volatile memory (internal storage), plus a removable storage medium such as a micro SD card (external storage). Some devices divide the permanent storage space into “internal” and “external” partitions, so even without a removable storage medium, there are always two storage spaces and the API behavior is the same whether the external storage is removable or not. The following lists summarize the facts about each storage space.
所有的安卓设备都有”内部”和”外部”两个存储位置。在安卓初期的时候,大多数设备提供了内置的非易失性存储器(内部存储)和类似SD卡的可移动存储介质(外置存储),有些设备把永久性存储空间划分成“内部”和“外部”两个分区,所以即使没有一个可移动存储介质,诸如SD卡,但总有两个存储空间,API的行为总是相同的,不论外部存储是否是可拆卸的。下面是对每个存储空间的总结:
Internal storage:
- It’s always available.
- Files saved here are accessible by only your app.
- 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.
内部存储器:
- 它总是可用的
- 存储在这里面的文件只能通过你的app访问
- 当用户卸载了你的app后,系统把保存在内部存储器里面的所有文件全部删除
文件存储在内部存储器,其他用户或app都不能当问。
External storage:
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.
外部存储器:
- 它有时候是不可用的,因为用户可以把它挂载为SUB存储器,或者在某种情况下会把它从设备移除
- 它是可供外部访问的,所以文件存储在这里可能会被读取,从而脱离你的掌控
- 当用户卸载了你的app,系统只卸载保存在通过getExternalFilesDir()获取的目录里面的文件
对于不需要访问权限,或者你想要同其他app分享,或者允许用户通过电脑访问,外部存储器是最好的存储文件的地方。
Tip: Although apps are installed onto the internal storage by default, you can specify the android:installLocation attribute in your manifest so your app may be installed on external storage. Users appreciate this option when the APK size is very large and they have an external storage space that’s larger than the internal storage. For more information, see App Install Location.
这个是说app默认安装在内部存储器,不过你可以在manifest文件里面通过android:installLocation属性指定安装在外部存储器。用户喜欢把大的APK安装在外部存储器。
To write to the external storage, you must request the WRITE_EXTERNAL_STORAGE permission in your manifest file:
...>
"android.permission.WRITE_EXTERNAL_STORAGE" />
...
Caution: Currently, all apps have the ability to read the external storage without a special permission. However, this will change in a future release. If your app needs to read the external storage (but not write to it), then you will need to declare the READ_EXTERNAL_STORAGE permission. To ensure that your app continues to work as expected, you should declare this permission now, before the change takes effect.
...>
"android.permission.READ_EXTERNAL_STORAGE" />
...
However, if your app uses the WRITE_EXTERNAL_STORAGE permission, then it implicitly has permission to read the external storage as well.
You don’t need any permissions to save files on the internal storage. Your application always has permission to read and write files in its internal storage directory.
当前,所有的app不需要指定权限即可以读取外部存储器里面的文件,然而,这个在不久就会改变。为了保证app可用,最好声明READ_EXTERNA_STORAGE权限。
如果你的app有WRITE_EXTERNAL_STORAGE权限,默认就会有读取外部存储器的权限。
内部存储器的读写不需要权限声明。
When saving a file to internal storage, you can acquire the appropriate directory as a File by calling one of two methods:
getFilesDir()
Returns a File representing an internal directory for your app.
getCacheDir()
Returns a File representing an internal directory for your app’s temporary cache files. Be sure to delete each file once it is no longer needed and implement a reasonable size limit for the amount of memory you use at any given time, such as 1MB. If the system begins running low on storage, it may delete your cache files without warning.
可以通过两个方法获取内部存储器保存文件的路径。
To create a new file in one of these directories, you can use the File() constructor, passing the File provided by one of the above methods that specifies your internal storage directory. For example:
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) {
}
写入字符串到文件内。
Or, if you need to cache some files, you should instead use createTempFile(). For example, the following method extracts the file name from a URL and creates a file with that name in your app’s internal cache directory:
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
}
}
缓存文件。
Note: Your app’s internal storage directory is specified by your app’s package name in a special location of the Android file system. Technically, another app can read your internal files if you set the file mode to be readable. However, the other app would also need to know your app package name and file names. Other apps cannot browse your internal directories and do not have read or write access unless you explicitly set the files to be readable or writable. So as long as you use MODE_PRIVATE for your files on the internal storage, they are never accessible to other apps.
你app的内部存储目录是在系统的特殊位置通过包名指定的。技术上说,如果你设为可读的,,另一个app可以根据你的包名和文件名读取你的内部存储文件。设为MODE_PRIVATE模式后,则不能被其他app读取。
Because the external storage may be unavailable—such as when the user has mounted the storage to a PC or has removed the SD card that provides the external storage—you should always verify that the volume is available before accessing it. You can query the external storage state by calling getExternalStorageState(). If the returned state is equal to MEDIA_MOUNTED, then you can read and write your files. For example, the following methods are useful to determine the storage availability:
/* 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;
}
由于外部存储器有可能不可用,比如正在挂载在PC上,或者移除了SD卡,所以用之前需要校验。
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.
外部存储器保存文件有两种内别:公有文件和私有文件。共有文件可被其他程序访问,卸载app后不会被删除,依然保留。私有文件技术上讲也是可以被其他程序访问的,所以不应该存储重要的数据,卸载app后,这些文件则会被删除。
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) {
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;
}
If none of the pre-defined sub-directory names suit your files, you can instead call getExternalFilesDir() and pass null. This returns the root directory for your app’s private directory on the external storage.