There is a problem that the external storage cannot be accessed in Android 11 or later

 What are you doing recently Android Application development, IDE yes android studio ,  The version configuration used is as follows:

compileSdk 32
buildToolsVersion '32.0.0'
defaultConfig {
    applicationId "com.example.gzpersonmanager"
    minSdk 21
    targetSdk 32
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

It can be seen that the Framework version used is 32.1.1. The API and SDK of the latest Android 12 version are used. A scenario involved in the development process is to write the APP's Sqlite database into the APP's internal data storage / data / packetname / files / databases / directory through external storage and use code, mainly for database security, To put it simply, the external Sqlite database file is copied to the APP private data/data / directory during APP startup to realize local database update.
However, an error was encountered during the process. The prompt was that you didn't have permission to access the external storage. At first, you thought you didn't have authorization and checked

AndroidManifest.xml, file and permission settings are as follows: there is no problem.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

There is no problem with the file path exposure mapping. The configuration is as follows: no problem!

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.gzpersonmanager.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/authfileinfo" />
    </provider>

The xml/authfileinfo file is configured as follows: the internal / data/data / package name / files and the external storage path are mapped to the real address.

no problem.

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path path="." name="files" />
    <external-files-path path="." name="sdcard" />
 </paths>

But for real machine debugging, you still have no permission to debug! Baidu many documents, all methods have been tested, still not.

Finally, I finally found an article that after Android 11, the official uses storage partition management. By default, all applications can access their own APP private internal storage, that is, / data / package name / * directory, and external stored media directory. The external stored media directory can be accessed after authorization. These directories include photo albums, music, and video directories, Other external storage directories are inaccessible, including sdcard/Android / and sdcard/Download directories. This is the main reason. It's easy to solve if you know the reason.

1. Using storage partition access, many posts on the Internet say that requestLegacyExternalStorage = "true" is OK. In fact, the second method is still invalid in versions with targetsdk > 29.
You can use the Scoped Storage adapter to access other directories of external storage, which is the first method.

2 is the method I use now. Change the Framework version to 28, that is, targetSdk:28, then delete the APP again, and then Sync Gradle project, which can perfectly solve this problem.

This problem can be solved perfectly now.

Keywords: Android Android Studio SQLite

Added by api on Thu, 27 Jan 2022 13:52:31 +0200