Realization of AndroidQ Partition Storage Authority Change and Adaptation

  • 2021-12-04 19:41:07
  • OfStack

Partitioned storage

Partitioned storage function is introduced in Android Q, and an "isolated storage sandbox" is provided for each application in external storage devices. Other applications cannot directly access the application's sandbox file. Because the file is the private file of the application, you no longer need any permission to access and save your own file. This change also helps to reduce the number of permissions required for the application, while ensuring the privacy of the user's files.

Permission change

Android Q changes the way applications access files (such as:/sdcard) in storage devices outside the device. Continue to use the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions, except that when you have these permissions, you can only access media files, not other files.

In earlier versions of beta, Android required to apply for specific media permissions: READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, READ_MEDIA_AUDIO, but in beta4, these permissions were discarded.

Access private files

The application needs to store files in the sandbox of the application, and there is no permission to access this folder. The official recommended location for storing files in the sandbox is the folder under Context. getExternalFilesDir ().
For example, if you want to get a picture,


Context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)

Access files created by other applications

Your application can access files created by other applications only if the following two conditions are met:
1. Your application has obtained READ_EXTERNAL_STORAGE permission.

2. These files are in one of the following well-defined media collections:
Photo: stored in MediaStore. Images.
Video: Stored in MediaStore. Video.
Music file: stored in MediaStore. Audio.

Any other files, including those in the "downloads" directory, must use the storage access framework

Note: Applications that enter filtered views when accessing files in external storage devices do not have direct kernel access to paths such as/sdcard/DCIM/IMG1024. JPG. To access such files, applications must use methods such as MediaStore. openFile ().

Retain applied files after uninstallation

Files are stored in the application's private directory, and after uninstalling the application, the system will clear all files in the application's directory (somewhat similar to the Android/data/xxx directory). Sometimes we want to keep these files after uninstalling, please save them to a directory in MediaStore.

Choose to disable partitioned storage

There are two ways to make partitioned storage work on Android Q devices:

Target Android 9 or earlier (Target SDK < = 28) If Target SDK > 28. Add android to manifest: requestLegacyExternalStorage= "true"

In this way, the original storage strategy can be adopted. The above method is not recommended.
Official warning: Next year, all major platform versions of applications will need partitioned storage, regardless of the target SDK level.

File access rights summary

文件位置 所需权限 访问方法 卸载时是否删除文件
应用私有目录 getExternalFilesDir()
媒体集合(照片、视频、音频) READ_EXTERNAL_STORAGE(仅当访问其他应用的文件时) MediaStore
下载内容(文档和电子书籍) 存储访问框架(加载系统的文件选择器

You can use the Storage Access Framework to access each of the locations shown in the above table without requesting any permissions.

Specific file access adaptation

Share media files

If your app needs to share photos and videos. Use MediaStore to store files that need to be shared.

If you provide 1 set of companion apps (such as text messaging apps and profile apps), use content://URI to set up file sharing. This workflow has been recommended as a security best practice.

Working with documents

If you need to open a corporate office document or open a book saved as an EPUB file.
By calling ACTION_OPEN_DOCUMENT, intent selects the file to open, and intent opens the system's file selector application. Displays the types of files supported by the application, which need to be included in intent Intent.EXTRA_MIME_TYPES extra

The ActionOpenDocument example on GitHub shows how to open a file using ACTION_OPEN_DOCUMENT.

Access and modify media content

As described above, we need to use MediaStore instead of repeating it.

Update media files for other applications

Android Q previous applications did not pay much attention to the access rights of other user groups to the application directory. After adapting Android Q, you will receive a questionnaire asking you to limit the access rights of user groups to the storage directory.
To modify a given media file that another application saved to an external storage device, capture the RecoverableSecurityException thrown by the platform. You can then ask the user to grant your application write permission for this specific content.

Location information in photos

The photos we took generally contain location information in Exif metadata. Before Android Q, we can easily obtain the location information of pictures. Android Q will hide this information from your application by default. And this position information restriction is different from the restriction applicable to camera functions.
If your application needs to access the location information of photos, please complete the following steps:

Add the new ACCESS_MEDIA_LOCATION permission to the application manifest.

Invoke setRequireOriginal () in an MediaStore object, passing in the photo's URI.


val photoUri = MediaStore.setRequireOriginal(photoUri)
  contentResolver.openInputStream(photoUri).use { stream ->
    ExifInterface(stream).run {
      // If lat/long is null, fall back to the coordinates (0, 0).
      val latLong = ?: doubleArrayOf(0.0, 0.0)
    }
  }

Related articles: