To publish on Google Play, developers need to update the app’s target API level (API level 30) (Android 11) or higher. The policy has been in effect since August for new apps; Existing apps will be updated with new versions of the policy, which will take effect in November.

One big change brought about by API 30 is that applications need to use Scoped Storage.

The scale of change is terrifying for large applications. To make matters worse, some of the advice we read online on how to adapt partitioned storage is confusing and even misleading.

To help you out, we’ve collected some common questions about partitioned storage, as well as suggestions and possible alternatives for how to adapt to your application.

Q: the android: requestLegacyStorage will be removed?

A: Partially removed.

If your application is currently set android: requestLegacyStorage = “true”, targetSdkVersion should be set up to 30 after the status quo. This tag has no effect on Android 11 devices, but allows the app to continue to access storage in the old way on Android 10 devices.

If you need for Android devices in AndroidManifest. XML set Android: requestLegacyStorage = “true”, the application in the target version to Android after 11 shall keep this setting. It will still work on Android 10 devices.

Q: the android: how preserveLegacyStorage work?

A: if your application is installed on the Android 10 equipment, and set the Android: requestLegacyStorage = “true”, in the equipment to upgrade to the Android after 11, this setting will continue to keep the old way of storage access.

⚠️ If the application is uninstalled, or installed for the first time on Android 11, the old storage access method cannot be used. This flag is only intended to further aid in upgrading devices from traditional storage to partitioned storage.

Q: If my application does not have access to photo, video, or audio files, do I still need to request the READ_EXTERNAL_STORAGE permission?

A: No. Starting from Android 11, you need to request the READ_EXTERNAL_STORAGE permission only to access media files belonging to other applications. If your application uses only non-media files it creates (or media files it creates), you no longer need to request this permission.

To stop requesting this permission after Android 11, simply change the < uses-Permission > tag in the androidmanifest.xml file of the application and add Android :maxSdkVersion=”29″ :

<uses-permission
   android:name="android.permission.READ_EXTERNAL_STORAGE"
   android:maxSdkVersion="29" />
Copy the code

Q: Do I have to use system File picker if I want to access photos, videos, or an audio clip that doesn’t belong to my app?

A: no. But you can use it if you want, ACTION_OPEN_DOCUMENT is available as early as Android KitKat (API 19) and ACTION_GET_CONTENT is available as API 1, both using system file selectors. Since no permissions are required, this is still the preferred solution.

If you don’t want to use the system File selector, you can still request the READ_EXTERNAL_STORAGE permission, which gives your application access to all photo, video, and audio files, as well as access to the File API!

If you need to use the File access media content API, remember to set the android: requestLegacyStorage = “true”, otherwise the File API 10 will not be able to work in android.

Q: I want to save non-media files, but I don’t want to delete them when I uninstall my application. Do I need to use SAF?

A: Maybe.

If these files can be opened outside your application without passing through your application, the System File selector is a good choice. You can create the file using ACTION_CREATE_DOCUMENT. You can also open an existing file using ACTION_OPEN_DOCUMENT.

If the application has created a directory to store all these files, the best choice is to use the system file selector and ACTION_OPEN_DOCUMENT_TREE so that the user can select a specific folder to use.

If these files are only meaningful to your application, you can consider the application AndroidManifest. XML file < application > tag set android: hasFragileUserData = “true”. This will enable the user to retain this data even when the application is uninstalled.

△ Above is an uninstall dialog box for an app with “vulnerable user data”. The dialog box contains a check box indicating whether the system should retain application data.

When this flag is set, the best place to store a file depends on its contents. Files containing sensitive or private information should be stored in the directory returned by Context#getFilesDir(); Insensitive data should be stored in the directory returned by Context#getExternalFilesDir().

Q: I can place non-media files in other folders (such as the Downloads folder) without any permissions. Is this a Bug?

A: no, it isn’t. Applications may provide files to such collections, and it is best to use both the Downloads and Documents collections for non-media files. Remember, however, that by default only the application that created the files can access them. Other applications require access through the system file picker or extensive access to external storage (MANAGE_EXTERNAL_STORAGE).

⚠️ Access to MANAGE_EXTERNAL_STORAGE is regulated by the Play policy.

Q: If I need to save a document, do I need to use SAF?

A: no. Applications can provide non-media files to Documents and Downloads collections without any special permissions. Applications that provide documents to these collections have full access to those documents as long as they are not uninstalled.

💡 If your application requests the READ_EXTERNAL_STORAGE permission to save documents in the manner mentioned above, it will not need to request this permission in Android 11 and later. You can modify the request for this permission (set maxSdkVersion to API version 29) by following the example below:

<uses-permission
   android:name="android.permission.READ_EXTERNAL_STORAGE"
   android:maxSdkVersion="29" />
Copy the code

To access documents added by other applications, or to access documents added before your application is unloaded or reinstalled, use the system file selector with ACTION_OPEN_DOCUMENT Intent.

Q: If I want to share files with other applications, do I need to use SAF?

A: No. Here are some ways to share files with other applications:

  • Direct shareUse:Intent.ACTION_SENDAllows your users to share data with other applications on the device in a variety of formats. If you use this approach, useThe AndroidX FileProviderIt might be helpful to automatically convert the file:// Uri to content:// Uri.
  • Create your own DocumentProvider: This allows your application to continue processing content in the application’s private directory (Context#getFilesDirs() or Context#getExternalFilesDirs()) while still providing access to other applications that use the system file selector. (please note that can be applied to the unloading after continue to hold these files – see above the android: hasFragileUserData = “true” Settings to understand its use.)

Final thoughts

Scoped Storage is a major change designed to improve user privacy protection. Still, there are many ways to work with content that doesn’t rely on using the Storage Access Framework.

If the data to be stored is only applicable to your application, we strongly recommend using application-specific directories.

If the data is media files, such as photos, video, or audio, use MediaStore. Note that starting with Android 10, you no longer need to request permission to serve content.

Also don’t forget to share data with other apps (or allow them to share data with your app) via ACTION_SEND!