Original: HuangBo-OEM Baidu APP technical team

background

1. Partition storage background

On September 3, 2019, Google released Android10. In order to better protect user data and limit the increase of redundant files on the device, Android10 has changed the external storage access mode of the device. The new external Storage feature is called Scoped Storage. Scoped Storage redesigns the access mode of external Storage files based on the following principles to facilitate the management of external Storage files.

1.1 Better attribution of files:

Which application creates the system record file? The application can access the file created by the application without storage permission

1.2 Application Data Protection:

Add restrictions on access to private directory files of external storage applications. Applications cannot access private directory files of external storage applications even after they have applied for the storage permission

1.3 User Data Protection:

Add restrictions on access to PDF, Office, and DOC files. Users cannot access PDF, Office, and DOC files created by other applications even after they have applied for storage permission

2. Apply compatibility mode

Partition storage has some adaptation costs, Google provides a transition solution for Android10, setting the application to run in compatibility mode (see 2.3 for details), Android11 will not support this behavior, each application needs to complete partition storage adaptation before the release of Android11.

This section describes the new feature of partitioned storage

1. Partition storage overview

1.1 Partitioned Storage The external storage is redesigned into two parts: application private directories and shared directories

(1) Private application directory: Private application data is stored. The private application directory corresponds to Android/data/ Packagename for external storage, and the private application directory corresponds to data/data/packagename for internal storage. (2) Shared directory: Stores files accessible to other applications, including media files, document files, and other files, corresponding to devices such as DCIM, Pictures, Alarms, Music, Notifications,Podcasts, Ringtones, Movies, and Download.

1.2 Applying Private Directory File Access

The application private directory file access mode is the same as that of the previous Android version. You can obtain resources through file path.

1.3 Accessing Files in a Shared Directory

Files in a shared directory can be accessed using the MediaStore API or Storage Access Framework. (1) The MediaStore API creates files in a specified directory in a shared directory or creates files for an application. (2) If the MediaStore API accesses media files (images, audio files, and videos) created in the shared directory by other applications, you need to apply for the storage permission. If you do not apply for the storage permission, the Uri cannot be queried through the ContentResolver. Reading or creating a file throws an exception; (3) MediaStore API cannot Access non-media files (PDF, Office, doc, TXT, etc.) created by other applications. It can only be accessed through Storage Access Framework. ;

1.4 Other affected changes

(1) Some images contain location information, because location is sensitive information for users, Android 10 apps cannot obtain location information by default in partitioned storage mode. Applications can obtain location information through the following two Settings: ACCESS_MEDIA_LOCATION in the manifest file. Call MediaStore setRequireOriginal(Uri Uri) to update image Uri. (2) Mediastore. Files In application partition storage mode, mediastore. Files can only obtain media Files (pictures, audio Files, and videos), but cannot obtain non-media Files (PDF, Office, doc, and TXT). This interface enables the new feature of partition storage. Andrioid 10 cannot directly access resources in the shared directory through the File Path. The following interfaces operate File resources through the File Path, and their functions are affected. The application can be accessed in MediaStore or SAF mode.

2. Storage Features Overview of Android version differences

3. Compatible mode

3.1 Compatibility Mode Setting

Before the external storage adaptation is complete, an application can run in compatibility mode temporarily. If the application applies for storage permission in compatibility mode, it can have access to the complete directory of the external storage and run through the file access mode before Android10. The following two methods are used to set the application to run in compatibility mode. (1) Target < Android 9 (API level 28); (2) the Tagret greater than or equal to 10 Android (API level 29, set in the manifest requestLegacyExternalStorage attribute to true.

<manifest ... > <! -- This attribute is"false" by default on apps targeting
       Android 10 or higher. -->
    <application android:requestLegacyExternalStorage="true". >... </application> </manifest>Copy the code

3.2 Determining compatible mode interfaces

Environment. IsExternalStorageLegacy () returns a value: true: applications to compatible mode; False: The application runs in partitioned storage mode. Remarks Storage adaptation has been completed and partition storage has been enabled for the application. If the current application runs in compatible mode, the application will still run in compatible mode after overwrite installation

Partition adaptation scheme

1. Partition storage adaptation scheme overview

Partition storage adaptation includes two parts: file migration and file access compatibility adaptation.

1.1 File Migration

File migration is to migrate files from the application shared directory to the application private directory or the media collection directory required by Android10. (1) For files that can be accessed only by the application itself and deleted after the application is uninstalled, you need to migrate the files to the private directory files of the application. File path can be used to access File resources to reduce adaptation costs. (2) Files that are allowed to be accessed by other applications and cannot be deleted after the application is uninstalled need to be stored in the shared directory. The application can choose whether to modify the directory and migrate the files to the media collection directory required by Android10.

1.2 File Access Compatibility and Adaptation

A shared directory File cannot be read using File Path and can be accessed using MediaStore API or Storage Access Framework.

2. Partition storage adaptation guide

2.1 MediaStore API Introduction

2.1.1 Description of the MediaStore API

The system automatically scans the external storage and adds the files to the Images, Videos, Audio files, and Downloaded files that the system has defined. Android 10 uses mediastore. Images, Mediastore. Video, mediastore. Audio, and mediastore. Downloads to access shared directory file resources

2.1.2 MediaStore API file Creation

The MeidaStore API for Android 10 only allows you to create files in the specified directory of the shared directory. An IllegalArgumentException will be thrown if you create a file in a specified directory.

note


2.1.3 Different Storage Permissions MediaStore API can access file areas

2.1.4 MediaStore API File Access

2.2 Storage Access Framework

Android 4.4 introduces Storage Access Framework, and applications Access documents provided by DocumentsProvider through system selector (including external Storage and cloud Storage, external Storage includes application private directory and shared directory). SAF mechanism does not need to apply for any storage permissions, including Document Provider, Client APP and Picker: Document provider is a subclass of DocumentsProvider. The data model is designed based on file hierarchy. Document provider manages files through storage service (such as Google Drive). (2) the Client app: Call ACTION_CREATE_DOCUMENT, ACTION_OPEN_DOCUMENT, and ACTION_OPEN_DOCUMENT_TREE Intent to retrieve the file provided by the Document provider. An application can set MIME Type or EXTRA_INITIAL_URI to select the file to fetch, and the onActivityResult interface returns the Uri of the selected file. (3)Picker: system UI. The application obtains the file information provided by the Document Provider by tuning the system selector.

2.3 Sharing Scenario Adaptation

(1) APP actively shares files with other applications, and can use FileProvider to grant other applications file reading permission. FileProvider applications generate file URIs based on XML configuration, and other applications can obtain file resources by receiving URIs without applying for storage permission. (2) If an Android 10 application enables partitioned storage and shares files with other applications through the File protocol Uri or MediaStore Uri, the function will be affected, as shown in the following table:

Baidu APP partition storage adaptation practice

1. Brief introduction of Baidu APP adaptation

Baidu APP partition storage adaptation involves more than 20 business parties. After three iterations of versions, 90% of the problems have been solved, including checking the resources accessed through File in the shared directory of external storage. For historical files, the business party can choose whether to migrate files based on specific scenarios. For files that are accessed only by the application itself and can be deleted after the application is uninstalled, migrate the files to private directories of external storage. For files that can be accessed by other applications and cannot be deleted after an application is uninstalled, use MediaStore API or SAF to adapt the files

2. Baidu APP business adaptation summary

Refer to the link

1. www.youtube.com/watch?v=unj… 2. developer.android.com/training/da… 3. developer.android.com/training/da… 4. developer.android.com/training/da… 5. developer.android.com/guide/topic…