1 background

Before Android 10, most applications only need to dynamically apply for READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions to create their own folders on the SD card. Even if an application is uninstalled, the files in this folder will not be deleted.

From a user perspective, if every App did this, the entire file system would be cluttered and the available space would be reduced (as a developer, I also feel unfriendly).

So, starting with Android 10, we introduced the concept of partitioned Storage: Scoped Storage.

The phone’s external storage is split into two parts: private directories app-specific and public directories. The goal is to standardize developers’ use of external storage and address these pain points.

The concept of partitioned storage

2.1 What is Partitioned storage?

As the name implies, the external storage space is divided into two areas:

  1. App-specific
  2. Public directory

2.1.1 App – specific

Also known as App external private directory. Features are cleared with uninstallation. But you can do this by adding attributes to the application tag:

Android: hasFragileUserData = "true" true: it means the pop-up dialog by default to false, don't pop up.Copy the code

When the user uninstalls the application, a dialog box is displayed asking the user whether to retain the application data. This gives the user the choice of whether or not to erase the data!

The counterpart of an external private directory is an internal private directory. Internal private directories can only be accessed by the application itself and are cleared with uninstallation. The path is as follows:

Access mode:

  • App can be accessed directly without applying for storage permission

  • Use SAF or FileProvider to share references with third parties

2.1.2 Public Directory

Include Downloads, Documents, Pictures, DCIM, Movies, Music, and Ringtones. When the application is uninstalled, files in these directories will not be deleted.

Access mode:

  • Media file: audio, video, and image. Through the MediaStore/SAF
  • Non-media documents: PDF/Documents. Through the SAF

SAF: System Access Framework. Generally used for file management applications.

3. Why do we have to adapt

  • When yourtargetSdkVersionSet to <=28:

When your App is running on version <=28, it will still be stored as before. When your App is running >= version 29, the system will activate compatibility behavior to ensure that it still works the way you want it to.

Although Android10 starts partitioning storage. However, the system will run your App in a compatible manner based on the targetSdkVersion you set.

  • When yourtargetSdkVersionSet it to >=29:

When your App is running on version <=28, it will still be stored as before.

When your App is running on version >=29, the system runs on partition storage by default. But we can set it in the Application tag:

Android: requestLegacyExternalStorage = "true" true: said in the original storage way false: said the partition storage Note: during the transitional period of the logo is only temporary solution. This will expire in Android R(11). Android R (11) and a new sign: preserveLegacyExternalStorage. True: In the case of overwriting the installation, the old storage mode is still used. Uninstallation is performed in partition mode.Copy the code

Conclusion:

  1. TargetSdkVersion remains below 29

You can always leave targetSdkVersion at <=28, but Google will force you to update it. Not only partition storage, Androidx upgrades and so on need you to upgrade.

  1. Temporary solution

But it’s also temporary. Drink poison to quench thirst belongs to is.

  1. Honestly partition storage

So how to fit, look down.

4 How to adapt

4.1 Scenarios affected by Adaptation:

1. Cannot create a folder on the SD card cause: Access to other file directories will fail except for app-specific external private directories. Solution: Create in an external private directory or create in a public directory through MediaStore.

2. Files in public directories cannot be accessed directly. Reason: Access to file directories will fail except for app-specific external private directories.

Problem: Use the MediaStore interface to access multimedia files in a public directory, or use SAF to access any file in a public directory.

Note: The DATA field queried from the MediaStore interface will be deprecated in Android Q and should not be used to access files or determine their existence;

Once you get the file Uri from the MediaStore interface or SAF, use the Uri to open the FD or input/output stream rather than converting it to a file path for access.

Cause 2: The MediaStore interface is used to access non-multimedia files.

Problem Analysis 2: On Android Q, only multimedia files in public directories can be accessed using the MediaStore interface.

3. Failure to share correctly

For example, wechat and QQ failed to share on Android 11. Reason: share files in the app-specific directory with the Uri starting with File://. This directory is not accessible to other apps. Solution: Therefore, you can only share with other apps in content: // format via FileProvide.

4.1 Storage Adaptation

Adaptation idea:

  1. Release a compatible version that copies the old data from the previous folder to the sandbox (optional, optional) when the application is started, if it is greater than 29 and partition storage is not enabled.
  2. According to the version > = 29 && Environment. IsExternalStorageLegacy = false judgment whether to enable the partition storage.
  3. If this function is disabled, the original compatibility mode is used
  4. If started, the sandbox directory is returned.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && ! Environment.isExternalStorageLegacy()) { sdCardDir = CoreValue.gContext.getExternalFilesDir(null); } else { sdCardDir = Environment.getExternalStorageDirectory(); } // use sdCardDir as a level 1 directory, and then create different sub-directories to store.Copy the code

MediaStore & SAF

OOPO adaptation

4.2 Sharing Adaptation

You can refer to the adaptation scheme of wechat and QQ:

Wechat ADAPTS to sharing through Provider

Wechat on Android11 partition storage adaptation

5 concludes

The overall idea of adaptation:

1. Process according to the SDK version. 29 Keep the original way below. 29 and above shall be treated by zones.

2. Try to use your own app-specific directory. Media files such as pictures, videos, and audio files that can be made public can be accessed through mediaStore.

3. For file management applications, you need to use SAF to access specified directories and files.

The above is the recent adaptation of some experience, if there are mistakes, but also hope to correct ~

Reference:

AndroidQ partition storage adaptation, AndroidR adaptation, and partition storage stomp summary

OOPO adaptation