To publish on Google Play, developers need to target API level (targetSdkVersion) to API level 30 (Android 11) or higher. For newly launched apps, this policy will take effect from August; existing apps are updated with new versions, and the requirements of this policy will take effect from November.
A huge change brought about by API 30 is that applications need to use Scoped Storage.
The magnitude of the changes is terrifying for large-scale applications. To make matters worse, some of the suggestions we have seen on the Internet about how to adapt to partition storage are very confusing and even misleading.
In order to help you solve problems, we have collected some common questions about partition storage, and also provided some suggestions and possible alternatives for how to adapt to your application.
Q: Will android:requestLegacyStorage be removed?
A: partially removed.
If your application currently has set android:requestLegacyStorage="true" , you should keep the status quo after setting targetSdkVersion
This flag has no effect on Android 11 devices, but you can continue to allow apps to access storage in the old way on Android 10 devices.
android:requestLegacyStorage="true"
in AndroidManifest.xml for Android 10 devices, you should keep this setting after the target version of the app is changed to Android 11. It will still take effect on Android 10 devices.
Q: How does android:preserveLegacyStorage work?
A: If your app is installed on an Android 10 device, and android:requestLegacyStorage="true"
set, this setting will continue to maintain the old storage access method after the device is upgraded to Android 11.
⚠️ If the app is uninstalled or installed on Android 11 for the first time, the old storage access method cannot be used. This mark is only applicable to further help the device upgrade from traditional storage to partitioned storage.
Q: If my application does not access photos, videos or audio files, do I still need to request the READ_EXTERNAL_STORAGE permission?
A: not required. Starting from Android 11, you only need to request the READ_EXTERNAL_STORAGE
permission when accessing media files belonging to other applications. If your application only uses non-media files created by itself (or media files created by itself), then you no longer need to request this permission.
If you need to stop requesting this permission after Android 11, you only need to modify 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" />
Q: I want to access photos, videos, or audio that are not part of my application. Do I have to use the system file selector?
A: No. But if you want to use it, you can use it. ACTION_OPEN_DOCUMENT
supports Android KitKat (API 19) at the earliest, and ACTION_GET_CONTENT
supports API 1. Both use the system file selector. 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 will allow your application to access all photos, videos and audio files. also includes the permission to access the File API!
If you need to use File API to access media content, remember to set android:requestLegacyStorage="true"
, otherwise File API will not work in Android 10.
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: may be needed.
If these files are allowed to be opened outside the application without going through your application, then the system file selector is a better choice. You can use ACTION_CREATE_DOCUMENT
create files. Of course, you can also use ACTION_OPEN_DOCUMENT
to open an existing file.
If the application has ever created a directory to store all these files, the best option is to use the system file selector and ACTION_OPEN_DOCUMENT_TREE
so that the user can select the specific folder to use.
If these files are only meaningful to your application, you can consider android:hasFragileUserData="true"
<application>
tag of the AndroidManifest.xml file of the application. This will allow users to retain this data even when the app is uninstalled.
△ The picture above shows the uninstall dialog of the application with "vulnerable user data". The dialog box contains a check box to indicate whether the system should retain application data.
Once the flag is set, the best location to store the file will depend on its content. Files containing sensitive or private information should be stored in Context#getFilesDir()
; insensitive data should be stored in the directory returned by Context#getExternalFilesDir()
Q: I can put non-media files in other folders (such as the Downloads folder) without any permissions. Is this a bug?
A: is not. Applications may provide files to such collections, and the best way is to use both the Downloads and Documents collections for non-media files. Remember, however, that only the application that created the file can access them by default. Other applications need to obtain access rights through the system file selector or have extensive access rights to external storage (ie: MANAGE_EXTERNAL_STORAGE
permissions).
⚠️ Access to the MANAGE_EXTERNAL_STORAGE
permission is regulated by the Play policy .
Q: If I need to save a document, do I need to use SAF?
A: not used. Applications can provide non-media files to the Documents and Downloads collections without any special permissions. As long as it is not uninstalled, applications that provide documents to these collections have full access to these documents.
READ_EXTERNAL_STORAGE
permission for saving documents in the above-mentioned way, it will no longer be required in Android 11 and higher. You can refer to the following example to modify the request for this permission (set maxSdkVersion to API version 29):
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />
To access documents added by other applications, or to access documents added before uninstallation after your application is uninstalled and reinstalled, you need to use the system file selector ACTION_OPEN_DOCUMENT
Q: I want to share files with other applications, do I need to use SAF?
A: required for 16128553b8037e. Here are some ways to share files with other apps:
- Direct sharing : Use
Intent.ACTION_SEND
to allow your users to share data with other applications on the device in various formats. If you use this method, using AndroidX's FileProvider to automatically convert file:// Uri to content:// Uri may help you. - Create your own DocumentProvider : This allows your application to continue processing the content in the application’s private directory (Context#getFilesDirs() or Context#getExternalFilesDirs()) while still providing it to other applications that use the system file selector access permission. (Please note that you can continue to save these files after uninstalling the app-see the android:hasFragileUserData="true" setting above to learn how to use it.)
Final thoughts
Scoped Storage is a major change designed to improve user privacy protection. However, there are still many ways to deal with content that does not rely on the use of the Storage Access Framework.
If the data to be stored is only applicable to your application, then we strongly recommend using application specific directory .
If the data is media files, such as photos, video or audio, you can use MediaStore . Note that starting from Android 10, providing content no longer requires requesting permission .
Also don't forget that you can share data through ACTION_SEND (or allow them to share data with your application )!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。