This is the 25th day of my participation in the August Genwen Challenge.More challenges in August

Overview of data and file storage

Android uses a file system similar to disk-based file systems on other platforms. The system provides the following options for saving application data:

  • Application-specific storage space: Stores files only for use. Files can be stored in a dedicated directory in an internal storage volume or another dedicated directory in an external storage space. Use directories in the internal storage space to store sensitive information that should not be accessed by other applications.
  • Shared storage: Stores files that your application intends to share with other applications, including media, documents, and other files.
  • Preferences: Stores private raw data as key-value pairs.
  • Database: Use the Room persistence library to store structured data in a dedicated database.

Application-specific storage space

A file for APP use only

  • Access methods

    • Access from internal storage space

      You can use the getFilesDir() or getCacheDir() methods

    • Access from external storage space

      The getExternalFilesDir() or getExternalCacheDir() methods can be used

  • The required permissions

    The file cannot be accessed if it is stored in a directory in the internal storage space. The file can be accessed if it is stored in a directory in the external storage space.

  • Are other applications accessible?

    • If the file is stored in a directory in internal storage space, it cannot be accessed
    • If the file is stored in a directory in external storage space, it can be accessed
  • Whether to remove files when uninstalling applications?

    is

File filesDir = getFilesDir();
File cacheDir = getCacheDir();
Log.d(TAG, "onCreate: filesDir = " + filesDir + ", cacheDir = " + cacheDir);
Copy the code

filesDir = /data/user/0/com.itdr.store/files, cacheDir = /data/user/0/com.itdr.store/cache

Media

Shareable media files (images, audio files, videos)

  • Access methods

MediaStore API

  • The required permissions

In Android 10 (API level 29) or later, The READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission is required for accessing files of other applications. In Android 9 (API level 28) or later, permissions are required for accessing all files

  • Are other applications accessible?

Yes, but other applications require the READ_EXTERNAL_STORAGE permission

  • Whether to remove files when uninstalling applications?

is

Documents and other files

Other types of shareable content, including downloaded files

  • Access methods

Memory access framework

  • The required permissions

There is no

  • Are other applications accessible?

Yes, it can be accessed through the system file picker

  • Whether to remove files when uninstalling applications?

no

Application Preferences

key-value

  • Access methods

SharedPreferences

  • The required permissions

There is no

  • Are other applications accessible?

no

  • Whether to remove files when uninstalling applications?

is

The database

Structured data

  • Access methods

Room Persistence library

  • The required permissions

There is no

  • Are other applications accessible?

no

  • Whether to remove files when uninstalling applications?

is

How much space does the data take up?

  • Internal storage spaceHas limited space to store application-specific data. If you need to store a large amount of data, use another type of storage space.

How reliable is data access required?

  • If certain data is required for basic application functions (for example, data required for application startup), you can save the data to an internal storage directory or database. Application-specific files stored in external storage space are not always accessible because some devices allow users to remove physical devices that provide external storage space.

What kind of data do YOU need to store?

  • If the data is only used by APP itself, it should be usedApplication-specific storage space. For shareable media content, use shared storage space so that other applications can access the content. For structured data, use preferences (for key-value pair data) or databases (for data with more than two columns).

Should data be used only by your application?

  • Should be used when storing sensitive data (data not accessible through any other application)Internal storage space, preferences, or databases. An additional advantage of internal storage is that the user cannot see the corresponding data.

Category of storage location

Android provides two types of physical storage locations: internal storage and external storage. ** ** On most devices, internal storage space is smaller than external storage space. However, internal storage space is always available on all devices, making it more reliable for storing the data on which applications depend.

Removable volumes (such as SD cards) belong to external storage space in the file system. Android uses paths (such as /sdcard) to represent these storage devices.

If the APK is very large, it can be declared in androidmanifest.xml

  <manifest ...
      android:installLocation="preferExternal" >
      ...
  </manifest>
Copy the code

Access to and required permissions for the external storage space

Android defines the following permissions for read/write access to external storage space: READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE.

On older versions of Android, apps need to declare these permissions to access any files that reside outside the app’s exclusive directory in external storage. The newer the Android system, the more it depends on the purpose of the file rather than its location to determine the application’s ability to access the file. This purpose-based storage model enhances user privacy because applications can only access areas they actually use in the device file system.

Partition storage

To help users better manage their files and reduce clutter, apps targeting Android 10 (API level 29) and later are granted partitioned access to external storage by default. ** An application can access only application-specific directories on the external storage space and specific types of media files created by the application.

Use partitioned storage unless your APP needs to access files stored outside the application-specific directory and directories accessible by the MediaStore API. If you store application-specific files in the external storage space, you can store these files in application-specific directories in the external storage space for easier partition storage. This way, your application will continue to have access to these files after partitioning storage is enabled.