All Android devices have two file storage areas: "internal" and "external" storage.
Internal storage
1. It is always available.
2. Only your application can access the files saved here.
3. When users uninstall your application, the system removes all files of your application from internal storage.
External storage
1. It is not available because the user can load external storage in the form of a USB storage device and remove it from the device in some cases.
2. It is globally readable, so the files saved here may be read out of your control.
3. When the user uninstalls your application, the system will remove the files of your application only if you save the files of your application in a directory through getExternal FilesDir ().
Note: External storage is the best location for files that do not require access restrictions and that you want to share with other applications or allow users to access computers.
Tip: Although the default external storage is applied, you can specify the android:installLocation attribute in your manifest file
Store files in internal storage
When saving files in internal storage, you can use one of the following two methods as the corresponding directory of File:
getFilesDir() | Returns File that represents the internal directory of your application |
---|---|
getCacheDir() | Returns File representing the internal directory of your application's temporary cache file. Be sure to delete all files that are no longer needed and implement reasonable size limits on the amount of memory you use at a given time, such as 1MB. If the system is about to run out of storage, it will delete your cached files without warning. |
If you want to create a new file in one of these directories, you can use the File() constructor.
File file = new File(context.getFilesDir,fileName);
File file = new File(context.getCacheDir,fileName);
You can also call openFileOutput() to get FileOutputStream for files written to an internal directory.
String fileName = "myFile";
String s = "hello world";
FileOutputStream fileOutputStream;
try {
fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
fileOutputStream.write(s.getBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
createTempFile() should be used instead if some files need to be cached.
public File getTempFile(Context context, String url){
File file = null;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir())//The first parameter is the file prefix, and the second parameter is the file suffix.
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
Save files in external storage
Since external storage may not be available, you can query the external storage status by calling getExternal Storage State (). If the return status is MEDIA_MOUNTED, then you can read and write your files.
//Check whether 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;
}
//Check whether external storage is available for at least reading
public boolean isExternalStorageReadable(){
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
return true;
}
return false;
}
External documents can also be set as public and private documents.
Public Documents
Documents that should be freely used by other applications and users. Users should still be able to use these files when they uninstall your application. For example, photos taken by your application or other downloaded files.
Private Documents
Files that belong to your application and should be deleted when users uninstall your application. Although these files are technically accessible to users and other applications (because they are stored in external storage), they do not actually provide any output value to users other than your application. When the user uninstalls your application, the system deletes all files in the external private directory of the application.
For example, other resources or temporary media files that your application downloads.
If you want to save a public file in an external storage device, use the getExternal Storage PublicDirectory () method to get the File that represents the corresponding directory on the external storage device. This method uses parameters that specify 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.
//Get the album storage directory
public File getAlbumStorageDir(String albumName){
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES),albumName);
if(!file.mkdirs()){
Log.e("file error","File creation failed");
}
return file;
}
If you want to save your application-specific files, you can get the appropriate directory by calling getExternal FilesDir () and passing it the name indicating the type of directory you want. Each directory created in this way will be added to the parent directory of all external storage files encapsulating your application, which will be deleted when the user uninstalls your application.
public File getAlbumStorageDir(Context context, String albumName){
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES),albumName);
if(!file.mkdirs()){
Log.e("file error","File creation failed");
}
return file;
}
If there is no predefined subdirectory name for the appropriate file, you can call getExternal FilesDir () and pass in null. This will return to the root directory of the dedicated directory of your application on external storage.
Query available space
Method | explain |
---|---|
getFreeSpace() | Current available space |
getTotalSpace() | Total space in storage volume |
Delete files
You should always delete files that are no longer needed. The most direct way to delete a file is to let the open file reference call delete().
myFile.delete();
If the file is stored in internal storage, you can also request Context to locate and delete the file by calling deleteFile():
myContext.deleteFile(fileName);
Note: When users uninstall your application, the Android system deletes the following items:
All files you save in internal storage
You use getExternal FilesDir () to save all files in external storage.
However, you should manually delete all cached files created periodically using getCacheDir() and periodically delete other files that are no longer needed.