Android11 and above file read and write permission application

Application for Android11 ​​read and write permissions
The Android11 ​​system imposes strict restrictions on application write permissions. This article describes how to obtain file read and write permissions. If the targetSdkVersion of build.gradle >= 29 in the project , there will be read and write problems.
When targetSdkVersion = 29, it can be solved by setting requestLegacyExternalStorage="true".
When targetSdkVersion = 30, you need to apply for all file permissions to obtain write permissions.

Add permission settings to AndroidManifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" /><!--add 2022-2-11 Android11-->

<application
    ...
    android:requestLegacyExternalStorage="true" 
    ...
    >

        
Code application for dynamic permissions
1. Apply for permissions above Android 6.0 and below Android 11
 

 if (Build.VERSION.SDK_INT >= 23) {// 6.0
        String[] perms = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE};
        for (String p : perms) {
            int f = ContextCompat.checkSelfPermission(SafeKeyToolActivity.this, p);
            Log.d("---", String.format("%s - %d", p, f));
            if (f != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(perms, 0XCF);
                break;
            }
        }
    }


Android11 ​​application permissions
Jump to the system setting interface, user authorization to obtain permissions.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !isRefuse) {// android 11  且 不是已经被拒绝
        // 先判断有没有权限
        if (!Environment.isExternalStorageManager()) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 1024);
        }
    }

// 带回授权结果
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1024 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    	// 检查是否有权限
        if (Environment.isExternalStorageManager()) {
            isRefuse = false;
            // 授权成功
        } else {
            isRefuse = true;
            // 授权失败
        }
    }
}


 

For some mobile phones such as Xiaomi mobile phones, even if the management authority of all files is obtained, the Android/data directory is still not accessible, as shown in the figure:

 

 

Try the solution:

Only use direct paths to read and write files in the app's private directory on external storage.
To access or share media files, use MediaStore to read and write files in the public directory.
To access or share non-media files, use the system file selector SAF to read and write files in the public directory Download.
You can create custom folders and files in the root directory.

 

Guess you like

Origin blog.csdn.net/yzwfeng/article/details/124378291