Detailed explanation of file storage in Android

  • 2021-12-11 18:51:27
  • OfStack

Catalog summary Stand-alone document Exclusive file internal storage external storage

Summary

When we look at the file manager of the mobile phone, we will find that there are 5 files and 8 files in it. It is very difficult to find the files corresponding to our own project, and it is even possible that we can't find our own files at all. This article will introduce the precautions for file storage in the development process of APP.

Usually, we will divide the stored files into two types: independent files and exclusive files. As the name implies, independent files are files independent of APP and will not be deleted with the deletion of APP, while exclusive files are files exclusive to an APP. When APP is deleted, the corresponding exclusive files will be automatically emptied.

Stand-alone document

Stand-alone files refer to the files stored under shared/external storage directory, which is usually our SD card. The SD card path can be obtained by:


Environment.getExternalStorageDirectory ()

It is not recommended to store files under the root directory of SD card, which will pollute the root directory of SD card of users. Usually, a secondary directory is established under the root directory and the files are stored under the secondary directory:


File sdCardDir = Environment.getExternalStorageDirectory ();
File cacheDir = new File(sdCardDir, "Cache");

The resulting path is as follows:


/storage/emulated/0/Cache

The paths of SD cards are different under different platforms, so developers usually hold a relative path.

Tips: The physical addresses of the external storage root directories (Environment. getExternalStorageDirectory ()) of different handset manufacturers are not identical, but their mapped root directories are usually 1, and the mapped root directories are:/mnt/sdcard/

There is another way to get the secondary directory of an SD card:


Environment.getExternalStoragePublicDirectory(String)

For this method, Android has given a set of fixed String parameters:


Environment.DIRECTORY_ALARMS
Environment.DIRECTORY_DCIM
Environment.DIRECTORY_DOCUMENTS
Environment.DIRECTORY_DOWNLOADS
Environment.DIRECTORY_MOVIES
Environment.DIRECTORY_MUSIC
Environment.DIRECTORY_NOTIFICATIONS
Environment.DIRECTORY_PICTURES
Environment.DIRECTORY_PODCASTS
Environment.DIRECTORY_RINGTONES
// ps: Call Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
//   The path you get is: /storage/emulated/0/Pictures

Personally, I prefer the first method, because the first method is more flexible and can customize the secondary directory.

Of course, before storing files to the SD card, you need to judge whether the SD card exists. The judgment method is as follows:


if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
  // SD Card presence 
}

Because you want to store a stand-alone file, you also need to add read and write permissions:


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

Usually, you only need to add write permission, and adding write permission adds read permission by default. These two permissions started from Android 4.4 (KITKAT)

Exclusive file

Private files can be stored in external storage or internal storage. external storage refers to SD card, and internal storage refers to the storage area of mobile phone. Some people may wonder: Didn't stand-alone files also be stored in SD cards before? How does the exclusive file also put in the SD card, and how does our APP distinguish between the two? Individual files are usually stored in the secondary directory of SD card, such as "Root/Cache" mentioned just now, while exclusive files are stored in the directory of "Root/Android/data/packge name", which will be automatically destroyed when APP is deleted.

Usually, we will store a small amount of data in internal storage, such as Shared Prefrence file or database file, and put a large amount of data in external storage, such as video and picture file.

Read and write permissions are not required to store exclusive files ~ ~

internal storage

The method for obtaining the storage path of APP in internal storage is as follows:


getCacheDir();
getFileDir();
getDir("name", mode);

The resulting path is as follows:


/data/data/<package name>/cache
/data/data/<package name>/file
/data/data/<package name>/"name"

This is the private path of APP. For mobile phones without root, users cannot access this path, so security is guaranteed (developers can access this path in Device File Explorer version 3.0 or above of AS). At the same time, the SD card of mobile phone is not available at all times, so we must store important data in internal storage.

According to Android SDK, files in the cache directory are automatically cleared when the phone is running out of disk space (other internal storage space other than cache is not automatically cleared). In this case, you need to store the cache in a non-cache folder as much as possible; Or control the file size under the cache folder under the system index. The method to obtain the system index is as follows:


 getCacheQuotaBytes(java.util.UUID)

If it is controlled below this metric, the cleanup priority of files will be the lowest (the metric is the Android 8.0 feature).

external storage

The method for obtaining the storage path of APP in external storage is as follows:


File sdCardDir = Environment.getExternalStorageDirectory ();
File cacheDir = new File(sdCardDir, "Cache");
0

The obtained path has also been mentioned before, as follows:


File sdCardDir = Environment.getExternalStorageDirectory ();
File cacheDir = new File(sdCardDir, "Cache");
1

Once you are familiar with the external storage path, you can also manually create the path from the root directory of the SD card:


File sdCardDir = Environment.getExternalStorageDirectory ();
File cacheDir = new File(sdCardDir, "Cache");
2

In addition to the video and picture files mentioned above, sometimes, developers also want to put some accessible files in the exclusive path of APP, such as crash or log log file of debug. At this time, internal storage cannot meet the demand (users cannot access internal storage and feedback specific logs), so these exclusive files can be placed in external storage directory.

The above is a detailed explanation of Android file storage details, more information about Android file storage please pay attention to other related articles on this site!


Related articles: