preface

Because two days before the development of an app update functionality, I will from the server to download the apk on the internal storage directory (test phones for millet, the path is: the data/user / 0 / packagename/files) below, and then has been installed is not installed, prompt error analysis package. Later, the query found that the installation of APK was called PackageInstaller, without relevant permissions, the internal path could not be obtained, so the installation could not be. Took the opportunity to review the following Android storage related knowledge points, especially to sum up some.

Classification of storage

Background of public account
015

Internal storage

Internal storage in the system in a very special position, for each App installed in the equipment, system in data/data/packagename/XXX is automatically created and the corresponding folder. If you want to store files in internal storage, the files can only be accessed by your application by default, and all files created by an application are in the same directory as the application package name. That is, the files created by the application in internal storage are associated with the application. When an application is uninstalled, these files are also deleted from the internal storage. This internal directory is inaccessible to the user unless root permission is obtained.

String fileDir = this.getFilesDir().getAbsolutePath();
String cacheDir = this.getCacheDir().getAbsolutePath();
Copy the code

Usually, we get to the path for the data/data/packagename/XXX, millet mobile phone printed below the results are as follows:

For internal storage paths, we generally use the following two methods to obtain the internal storage space using Context:

context.getFileDir()

Corresponds to the path of the internal storage is: the data/data/packagename/files, but for some phones such as huawei, millet etc. Access to the path for: data/user / 0 / packagename/files

context.getCacheDir()

The internal storage path is as follows: Data/data/packagename/cache, but for some phones such as huawei, millet etc. Access to the path for: Data/user / 0 / packagename/cache application cache directory, the directory file within the memory in the device will first be removed, so there is no any guarantee of stored in the file here, may be lost at any time.

External storage

External storage is a bit confusing, because before Android4.4, the phone’s body storage was called internal storage, and the SD card inserted was external storage, but after Android4.4, for now, the phone’s built-in storage is very large, and now with Android10.0, Some mobile phones can reach 256G of storage, in this case, mobile phone body built-in storage is also external storage, if the SD card is also called external storage, so external storage is divided into two parts: SD card and expansion card memory

We used a code to retrieve the external storage directory of the phone. Our test phone was a Samsung G4 with an SD card inserted:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            File[] files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            for (File file : files) {
                Log.e("file_dir", file.getAbsolutePath()); }}Copy the code

For the above code, print the following:

Extending external storage

The directory path needs to be retrieved from the context, and these files will also be deleted after the app is uninstalled. Similar to internal storage.

getExternalCacheDir()

Corresponding external storage path: / storage/emulated / 0 / Android/data/packagename/cache

getExternalFilesDir(String type)

Corresponding external storage path: / storage/emulated / 0 / Android/data/packagename/files

SD card storage

The files on the SD card are freely accessible, that is, the file data is accessible to other applications or users. After an application is uninstalled, the files created before the uninstallation remain.

You need to obtain the file path on the SD card from the Environment and determine the SD status before obtaining the file path:

MEDIA_UNKNOWN Indicates that the SD card is unknown

MEDIA_REMOVED AN SD card

MEDIA_UNMOUNTED The SD card is not installed

MEDIA_CHECKING SD card When the SD card is newly installed

The MEDIA_NOFS SD card is blank or is using an unsupported file system

MEDIA_MOUNTED The SD card is installed

MEDIA_MOUNTED_READ_ONLY The SD card is installed but read-only

MEDIA_SHARED Shared SD card

MEDIA_BAD_REMOVAL Error in removing an SD card

MEDIA_UNMOUNTABLE An SD card exists but cannot be mounted. For example, the media is damaged

 String externalStorageState = Environment.getExternalStorageState();
 if(externalStorageState. Equals (Environment MEDIA_MOUNTED)) {/ / sd card has been installed, can undertake relevant file operations}Copy the code
getExternalStorageDirectory()

External storage path :/storage/emulated/0

getExternalStoragePublicDirectory(String type)

Obtain the shared folder path of the external storage as follows:

DIRECTORY_MUSIC Music directory

DIRECTORY_PICTURES Picture directory

DIRECTORY_MOVIES Movies directory

DIRECTORY_DOWNLOADS Download directory

DIRECTORY_DCIM Directory for storing camera photo or video files

DIRECTORY_DOCUMENTS Indicates the document directory

String externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();
Copy the code

This is to get the camera DCIM directory, corresponding access path for: / storage/emulated / 0 / DCIM.

System Storage Directory

getRootDirectory()

Obtain the system partition root path /system

getDataDirectory()

The path to obtain user data is /data

getDownloadCacheDirectory()

Obtain the user cache directory path :/cache

Difference of related concepts

GetFileDir () and getCacheDir()

Both are located at the same level under the internal storage directory /data/data/packagename/. The former is under the file directory and the latter is under the cache directory.

GetFileDir () differs from getExternalFilesDir(String type)

The former is located in the internal storage directory/data/data/packagename/file below, which is located on the external storage directory/storage/emulated / 0 / Android/data/packagename/files below, they all exist in the application package name below, In other words, they belong to the app, so when the app is uninstalled, they will also be deleted.

For the app download and upgrade function mentioned above, the app downloaded from the server side needs to be placed under the external storage directory, not the internal storage directory, because the internal storage directory has very little space. In addition, I have also done relevant tests. If APK is placed under the internal storage directory file, there will be problems during installation, prompting parsing package error.

The difference between clearing data and clearing the cache

In app, there are two concepts of clearing data and clearing cache. What directories do these two concepts respectively clear data?

Clear data

Clear all data stored in the app, that is, all files under packagename mentioned above, Contains internal storage (/ data/data/packagename) and external storage (/ storage/emulated / 0 / Android/data/packagename). Of course, except for the data on the SD card, the data on the SD card will still exist after the app is uninstalled.

Clear the cache

Cache is a temporary storage space of the program is running, it can store temporary images downloaded from the Internet, from the perspective of users to clear the cache for the user is not much, but after clear the cache users using the APP again, because the local cache has been clean up, all of the data needs to be obtained from the Internet. To properly clear application-specific caches when clearing the cache, store the cache file in getCacheDir() or getExternalCacheDir().

This is the Android system storage directory management some knowledge.

About the author

Focus on Android development for many years, like to write blog record summary learning experience, blog synchronous update in my public number, welcome everyone to pay attention to, exchange learning ~