Detailed graphic explanation of Android internal storage and external storage (private directory and public directory)

   

Table of contents

1. Overview of storage space

Second, the division of storage space

1. Storage division

2. Internal storage

2.1 Internal Storage Overview

2.2 Internal Storage - Private Directory

3. External storage

3.1 Overview of external storage

3.2 External Storage - Private Directory

3.3 External Storage - Public Directory

3. Comparison of internal storage and external storage

1. Horizontal comparison

2. Directory structure

3. Storage classification

Four. Summary


    The user data, logs, downloaded pictures, files, etc. generated by the application during the running of the Android system need to be stored in the storage space provided by the Android system, so it is necessary to understand the concept, classification, characteristics and use of storage space under the Android system method, and save data in a suitable way in practical applications.

1. Overview of storage space

In terms of storage media, Android’s storage space is used for persistent data storage, which belongs to ROM storage media. Data will not be lost when the phone is turned off or the app is exited. Here, it needs to be conceptually distinguished from the     often mentioned “memory”: memory belongs to RAM storage medium, the data will be lost after exiting the App or shutting down. In the process of developing Android applications, we cannot avoid the use of data persistence technology. The so-called data persistence is to permanently save the temporary data in RAM to ROM to ensure that the data will not be lost after the app exits or the phone is turned off. .

    In terms of storage structure, the kernel of the Android system uses the Linux kernel, so the file directory structure of Android is similar to that of the Linux system. The Android system uses a virtual file system (VFS). The VFS directory uses "" /"as the root node, and there are different nodes under the root node. For example: /data, /sytem, ​​/mnt, /storage and so on.

Second, the division of storage space

    Our commonly used data persistence methods include file storage, database storage, SharedPreference storage, etc. There are two locations in the Android system for applications to implement persistent data storage: internal storage and external storage .

1. Storage division

    Before Android 4.4, the body storage of the device was internal storage, and the external SD card inserted to make up for the lack of internal storage space was called external storage.

    Among the devices after Android 4.4, many mid-to-high-end machines have expanded their body storage to more than 8G, conceptually dividing the same storage space into two parts: internal storage and external storage. , but in fact they are all inside the phone. Of course, you can still insert an SD card to expand the storage space, this part of the storage space is called extended external storage space. It's just that the body storage is relatively large now, and SD cards are rarely inserted.

     The display of the above two pictures merged into one picture:

2. Internal storage

2.1 Internal Storage Overview

    As mentioned earlier, the Android system takes "" /"as the root node, and there are different nodes under the root node, such as: /data, /sytem, ​​/mnt, /storage, etc. If the internal storage is logically distinguished by directories, it is /data The data folder under the directory: /data/data , ordinary users have no right to access this directory, and users need the ROOT permission of the mobile phone to view it. However, developers can use Android Studio's View ---- Tool Windows ---- Device File Explorer tool to view the directory, the approximate structure of the internal storage directory is shown in the figure below.

2.2 Internal Storage - Private Directory

2.2.1 Overview

    As can be seen from the figure above, the /data/data directory is organized according to the package name of the application. After each application is successfully installed, a new directory ( data/data/package-name ) will be automatically created, and the directory name is The package name of the application, so each application has its own internal storage directory. When the application is uninstalled, this directory will be automatically deleted by the system. Therefore, if you store data in internal storage, you are actually storing the data in the internal storage directory corresponding to your application package name.

    The internal storage directory of each application is private, which means that the files in the internal storage directory can only be accessed by the host application, and other applications have no permission to access. The host application does not need to apply for any permissions when accessing its own internal storage directory. Therefore, this part of storage is also called: internal storage private directory .

A typical internal storage private directory structure is as follows, and users can also create new directories according to their needs:

  • app_webview: Used to store data during webview loading, such as Cookie, LocalStorage, etc.
  • cache: Used to store cached data generated during the use of the application.
  • code_cache: stores the cache generated by runtime code optimization, etc.
  • databases: mainly used to store data of database type.
  • files: files can be stored in this directory.
  • lib: stores the so libraries that the App depends on.
  • shared_prefs: used to store SharedPreference files.

2.2.2 Features

  • The life cycle is the same as that of the host App. When the app is uninstalled, it will be automatically deleted by the system.
  • The host app can be directly accessed without permission;
  • Other applications do not have access;
  • User access requires Root authority.
  • It is suitable for storing data that is directly related to applications and has high privacy or sensitivity.

2.2.3 API related

# 获取的目录是/data/data/package_name,即应用内部存储的根目录
context.getDataDir();

# 获取的目录是/data/data/package_name/files,即应用内部存储的files目录
context.getFilesDir();

# 获取的目录是/data/data/package_name/cache,即应用内部存储的cache目录
context.getCacheDir();

# 获取的目录是/data/data/package_name/name,如果该目录不存在,系统会自动创建该目录。
context.getDir(String name, int mode) 

# 不同的mode
MODE_APPEND:即向文件尾写入数据
MODE_PRIVATE:即仅打开文件可写入数据
MODE_WORLD_READABLE:所有程序均可读该文件数据,Api 17废弃
MODE_WORLD_WRITABLE:即所有程序均可写入数据,Api 17废弃

3. External storage

3.1 Overview of external storage

    In layman's terms, the external storage space is what we see after opening the "file management" of the mobile phone system. The outermost directory of the external storage is the storage folder, or it can be the mnt folder. Different manufacturers will have different results . Generally speaking, there is an sdcard folder in the storage folder. Different from internal storage, external storage can be divided into three types according to different storage characteristics: private directory, public directory, and other directories . Among them, the " private directory " belongs to the " private storage space " of the external storage , and the " public directory " and " other directories " belong to the " shared space " of the external storage .

    Generally speaking, the persistent data involved in the application is divided into two categories: application-related data and application-independent data . The former refers to the data information exclusively used by the host App, such as some application configuration information, database information, cache files, etc. When the application is uninstalled, this information should also be deleted to avoid unnecessary occupation of storage space, and it is suitable to be placed in a " private directory " (internal storage or external storage). The latter is more inclined to this kind of information: when the application is uninstalled, the user still wants to keep the information in the device. For example, picture files of camera applications are manually downloaded by users using a browser. Application-independent data should be the data that the host application wants to share with other applications, and it is suitable to be stored in the " public directory " or " other directory " of the external storage space.

  • Private directory : The Android folder in the above picture. After opening this folder, there is a data folder inside. Open this data folder, and there are folders composed of many package names inside. These folders are the private directories of the application.
  • Public directory : Folders created for us by systems such as DCIM, Download, Music, Movies, Pictures, Ringtones; files in these directories can be shared by all applications.
  • Other directories : parts other than private and public directories. For example, the directories created by each app under the /sdcard/ directory, such as the directory created by Alipay: alipy/, the directory created by Weibo: com.sina.weibo/, the directory created by qq: com.tencent.mobileqq/, etc.

3.2 External Storage - Private Directory

3.2.1 Features

  • The life cycle is the same as that of the host App. When the app is uninstalled, it will be automatically deleted by the system.
  • The host App can be directly accessed without permission. (Note: Starting from version 4.4, the host app can directly read and write the application private directory in the external storage space. Before version 4.4, developers need to apply for file read and write permissions in the external storage space in the Manifest.)
  • Other apps can access it. (Remarks: Since Android 7.0, the system has further restricted access to the application's private directory. Other apps cannot directly read and write the file content in this directory through the Uri of the file:// form, and need to access it through FileProvider.)
  • Users can directly access without permission.
  • It is suitable for storing data that is directly related to the application and has low privacy or sensitivity.

3.2.2 API related

Similarly, the Android SDK also provides a convenient API for developers to directly operate the application private directory under the external storage space:

# 获取到的目录是 /storage/emulated/0/Android/data/package_name/cache
Context.getExternalCacheDir() 

# 如果type为"",那么获取到的目录是 /storage/emulated/0/Android/data/package_name/files,  如果type为"test",那么就会创建/storage/emulated/0/Android/data/package_name/files/test目录
Context.getExternalFilesDir(String type) 

3.3 External Storage - Public Directory

3.3.1 Features

  • It has nothing to do with the life cycle of the host App. After the app is uninstalled, the data is still retained;
  • All apps need to apply for EXTERNAL_STORAGE permission, and Android 6.0 needs to apply for dynamic permission;
  • User access, no permissions required.
  • Suitable for storing non-sensitive data and wanting to share data with other applications.

3.3.2 API related

# 获取到的目录是/storage/emulated/0,这个也是外部存储的根目录。
Environment.getExternalStorageDirectory() 

/* 
1.如果type为"",那么获取到的目录是外部存储的根目录即  /storage/emulated/0
2.如果type为"test",那么就在外部存储根目录下创建test目录,android官方推荐使用以下的type类型,我们在Sdcar的根目录下也经常可以看到下面的某些目录:
public static String DIRECTORY_MUSIC = "Music";
public static String DIRECTORY_PODCASTS = "Podcasts";
public static String DIRECTORY_RINGTONES = "Ringtones";
public static String DIRECTORY_ALARMS = "Alarms";
public static String DIRECTORY_NOTIFICATIONS = "Notifications";
public static String DIRECTORY_PICTURES = "Pictures";
public static String DIRECTORY_MOVIES = "Movies";
public static String DIRECTORY_DOWNLOADS = "Download";
public static String DIRECTORY_DCIM = "DCIM";
public static String DIRECTORY_DOCUMENTS = "Documents";
*/
Environment.getExternalStoragePublicDirectory(String type) 

3. Comparison of internal storage and external storage

1. Horizontal comparison

2. Directory structure

3. Storage classification

Four. Summary

    In general, if your application needs to store " highly private and application-related " data, you should choose a private directory for internal storage, and " less private but application-related " data should choose external storage Both of the private directories will be automatically deleted by the system after the app is uninstalled. If " the privacy is not high and shared with other apps, and you want to keep it after the app is uninstalled ", you should choose the public directory or other directories of external storage.

Question : I have read many articles saying that because the "internal storage" space is limited or relatively small, the "internal storage" should only store some files that occupy a small space, and try to choose the "external storage" space for large files?

    The author believes that the internal storage and external storage are now on the same storage medium, unless the system deliberately restricts them, the upper limit of the storage space of the two should only be limited by the total capacity and remaining capacity of the storage medium. In order to verify, after connecting the mobile phone through Adroid Studio, the author executes the command in the terminal: adb shell df -h to check the size of the partition. The total ROM storage space of the author's mobile phone is 256G, and the /data space is allocated 226G from the bottom, which is the same as the space of /storage/emulated and /mnt/mdfs/sdcard below. So there should not be a situation where the internal storage space is limited to a small size, it should be that "internal storage" and "external storage" share the same size storage space.

     You can see that the remaining storage space is 117G, which is consistent with the storage information I checked on the phone:

    For further verification, I deliberately made the total usage of the Cache files in the internal storage private directory exceed 1G in size, and there was no situation where the system automatically cleared the files because the total capacity of the internal storage files was too large. If you still feel uneasy, you can also choose to store data in "external storage". In addition, because "internal storage" is invisible to users (ROOT permission is required), if you want to browse or operate some data of the application more conveniently, you should choose to store such data in "external storage" "Private Directory" or "Other Directory".

    In addition, starting from Android 10, a new attribute has been added to the Manifest: android:hasFragileUserData="true" , ​​if the value here is "true", when the app is uninstalled, the user can check whether to keep the data in the pop-up dialog box . If checked, then the data in "External Storage - Application Private Directory" will be retained and will not be cleared by the system, which can be regarded as a more humane side of the Android system. But no matter whether it is checked or not, the data in the internal storage private directory will be deleted by the system.

Guess you like

Origin blog.csdn.net/crazestone0614/article/details/130773818