Android 11 Storage Adaptation Lite Edition

I read many articles on the Internet that adapt to Android 11, and I feel that they are too complicated, so I briefly list a few storage changes in Android 11 version.

1. Like other versions, these two directories are private directories of the app, and no permission is required to read and write.

/data/data/package

/sdcard/Android/data/package

 

2 . Read / create / delete external storage of multimedia catalogs, such as pictures, video, audio, download files, documents and so on. It is recommended to dynamically apply for the READ_EXTERNAL_STORAGE permission (this is the read permission, if you do not apply, the files read only include the files created by the app itself , and you can create/delete multimedia files without applying for any permissions), and you can access it through MediaStore.

 *Note 1. You cannot delete files created by other applications (the system will record which file is generated by your app). If you want to delete files written by other applications, a pop-up box will prompt the user every time you delete them.

                 2. The multimedia files are placed in a private directory, and the system cannot scan them, and they will not appear in the album. This is the same as the old version of the system.

                 3. The READ_EXTERNAL_STORAGE permission can only access externally stored multimedia files, and other types of files cannot be accessed.

                 4. If you want to get the shooting location information stored in the picture, you need to read it by ExifInterface.ACCESS_MEDIA_LOCATION权限,使用

 

3. External storage  write/delete files (all files in the sdcard, including multimedia files, including private directories of other applications), you need to request a new permission, the write permission is abolished in the new system, you must manually go to Open in the application settings (this time it is not a pop-up window).

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

Open method: // version should be judged when using, because low version system does not have this permission

val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                intent.data = Uri.parse("package:" + context!!.packageName)
                startActivityForResult(intent, 3)

 

4. There is no need to read or write permissions to select a file through the file selector. The user is guided to select the file, and the app accepts the callback method. The file selector must be the SAF framework that comes with the Android system , not the kind of open source or custom selector!

The file selector can not only read files, but also create files. These operations require the cooperation of the user to complete, not silently! (So ​​maybe the user experience is not good, the advantage is that no permission is required)

 

5. Application upgrade and version migration 

For apps that have been installed on version 11 (installed before 11, the system version has been upgraded), the target of the old version of the app is below 30, and after the upgrade, it is 30. Add the preserveLegacyExternalStorage flag and the application can also read and write in the way of legacy storage.

If it is a newly installed app on version 11 , even if there is preserveLegacyExternalStorage, it will not work!

 

This article introduces a general overview of storage. For specific code operations, please refer to the following blog:

https://developer.android.google.cn/training/data-storage/shared/media#java

https://www.jianshu.com/p/4d74b719309f

https://www.jianshu.com/p/e94cea26e213

 

Guess you like

Origin blog.csdn.net/gue8848/article/details/115277730