Android storage space summary

Overview

Android was stored before 6.0, which can really be described as "confusing".
There are many reasons for this, personally think there are the following points:

  1. In the early days of Android, the internal memory of mobile phones was very small, and most of the reading and writing of apps were performed in the SD card.
  2. User permission awareness is weak. Before Android 6.0, users directly applied for permission during installation, and users often gave storage permission without knowing it.
  3. In order to make its own application data look good, the app directly stores the resources to the SD card, and the files stored in the SD card will not be calculated as the memory usage of the app.
  4. The app is in a seller's market. If users are not given storage permissions, they cannot use some or all of the app's functions.

At present, as Android becomes more and more perfect, the official also has its own recommended storage solution. As an android developer, I have a better understanding.

Official documents: access application-specific files

The author summarizes this article based on my daily use experience and official documents. If there is any misunderstanding, please comment and exchange.

Pros and cons of storage solutions

Internal storage

Only you can access it, not other applications. No permission is required for self-access.

Android api is generally: context.getCacheDir() and context.getFilesDir()

scenes to be used

  • Mini programs and mini games
    (persistent files, suitable for storage in context.getFilesDir()) are
    associated with the app, and are useless after the app is uninstalled.
  • Cached pictures and videos
    (temporary files, suitable for storage in context.getCacheDir())
    can be cleaned up at any time. After cleaned up, the cold start logic is used.

Advantage

  1. There is no need to apply for permission, and the user has no perception.
  2. It will not affect phone storage, app deletion, that is, related resources are deleted together.

Disadvantage

  1. Cannot be stored permanently, the app will be deleted after it is deleted.
  2. When the android phone's storage is insufficient, the application cache files will be deleted to reclaim the space.

External storage

After obtaining read and write permissions, everyone can read and write.

Android api is generally: Environment.getExternalStorageDirectory()

scenes to be used

  • Local storage of photos, videos, documents, etc.
    need to be stored externally for users to share and use.

Advantage

  1. Unless the user actively deletes it, it can always be stored in the user's mobile phone.
    (There are many uses for this. For example, multiple applications can share a resource, instead of maintaining a copy for each application; when the application is deleted for the second installation, it can retain some user data and cache)
  2. After obtaining permission, it also means that you can read other content in the phone, such as photos, videos, etc.

Disadvantage

  1. Need to apply for user permissions, some users will choose to refuse.
  2. After the sd card is removed, it cannot be accessed.
    This problem is mainly in the older models, most mobile phones currently do not have SD cards.
  3. Because the data in the external storage will not be automatically deleted when the app is deleted, it will become the user's "storage garbage".

Common storage solutions

Code

  private void testAndroidFile() {
    
    
    Log.d(TAG, "Environment.getDataDirectory() filepath:" + Environment.getDataDirectory());
    Log.d(TAG, "Environment.getDownloadCacheDirectory() filepath:"
        + Environment.getDownloadCacheDirectory());
    Log.d(TAG, "Environment.getRootDirectory() filepath:" + Environment.getRootDirectory());
    Log.d(TAG, "Environment.getExternalStorageDirectory() filepath:"
        + Environment.getExternalStorageDirectory());
    Log.d(TAG, "context.getCacheDir() filepath:" + getCacheDir());
    Log.d(TAG, "context.getDataDir() filepath:" + getDataDir());
    Log.d(TAG, "context.getFilesDir() filepath:" + getFilesDir());
    Log.d(TAG, "context.getObbDir() filepath:" + getObbDir());
    Log.d(TAG, "context.getNoBackupFilesDir() filepath:" + getNoBackupFilesDir());
    Log.d(TAG, "context.getCodeCacheDir() filepath:" + getCodeCacheDir());
    Log.d(TAG, "context.getExternalCacheDir() filepath:" + getExternalCacheDir());
    Log.d(TAG, "context.getExternalFilesDir(null) filepath:" + getExternalFilesDir(null));
  }

Print results (mobile phone: Xiaomi 9, Android 10, MIUI 12)

Environment.getDataDirectory() filepath:/data
Environment.getDownloadCacheDirectory() filepath:/data/cache
Environment.getRootDirectory() filepath:/system
Environment.getExternalStorageDirectory() filepath:/storage/emulated/0
 context.getCacheDir() filepath:/data/user/0/com.example.androidfiletest/cache
context.getDataDir() filepath:/data/user/0/com.example.androidfiletest
 context.getFilesDir() filepath:/data/user/0/com.example.androidfiletest/files
 context.getObbDir() filepath:/storage/emulated/0/Android/obb/com.example.androidfiletest
context.getNoBackupFilesDir() filepath:/data/user/0/com.example.androidfiletest/no_backup
context.getCodeCacheDir() filepath:/data/user/0/com.example.androidfiletest/code_cache
context.getExternalCacheDir() filepath:/storage/emulated/0/Android/data/com.example.androidfiletest/cache
context.getExternalFilesDir(null) filepath:/storage/emulated/0/Android/data/com.example.androidfiletest/files

Guess you like

Origin blog.csdn.net/Double2hao/article/details/109269712