'manage external storage permission for android 11 issue

hi everybody i want to add storage permission for android 11 and above to my code despite of adding the bellow permissions

<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="29"
    tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
    tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

in the manifest i can't acces to storage for android 11 can you give me the necessary code to add in this Permission manager bellow other android version 10 and bellow are work good but 11 isee the permission dialog and i can accept it but i can't acces to media and files like isn't allowed

public abstract class PermissionsManager implements ActivityCompat.OnRequestPermissionsResultCallback {
private Activity activity;
private boolean grantedPermissions;
private String[] permissions;
private int requestCode;

protected PermissionsManager(Activity activity) {
    this.activity = activity;
    ((MainActivity) activity).setOnRequestPermissionsResultListener(this);
}

private boolean notGrantedPermission(String permission) {
    return ContextCompat.checkSelfPermission(activity, permission) != PackageManager
            .PERMISSION_GRANTED;
}

public void checkPermissions(String permission, int requestCode) {
    try {
        checkPermissions(new String[]{permission}, requestCode);
    }//end try
    catch (Exception e){
        e.printStackTrace();
    }// end catch
}

public void checkPermissions(String[] permissions, int requestCode) {
    try {
        this.permissions = permissions;
        this.requestCode = requestCode;
        for (String permission : permissions) {
            if (notGrantedPermission(permission)) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
                    showRequestPermissionRationale();
                } else {
                    requestPermissions();
                }
                break;
            } else grantedPermissions = true;
        }
        if (grantedPermissions) onPermissionsGranted();
    }//end try
    catch (Exception e){
        e.printStackTrace();
    }// catch
}

public void requestPermissions() {
    ActivityCompat.requestPermissions(activity, permissions, requestCode);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
    for (int i = 0; i < permissions.length; i++) {
        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    permissions[i])) {
                grantedPermissions = false;
                requestDisallowedAction();
            } else {
                grantedPermissions = false;
                onPermissionsDenied();
            }
            break;
        } else grantedPermissions = true;
    }
    if (grantedPermissions) onPermissionsGranted();
}

/**
 * add code here to tell users what permissions you need granted and why you need each
 * permission. Should call requestPermissions() after showing rationale.
 */
public abstract void showRequestPermissionRationale();

/**
 * add code here when permissions can't be requested. Either disable feature, direct user to
 * settings to allow user to set permissions, ask user to uninstall, or etc.
 */
public abstract void requestDisallowedAction();

public abstract void onPermissionsGranted();

public abstract void onPermissionsDenied();

}



Solution 1:[1]

yes its true. While compare to Android 11 and Android 10 there is a lot of changes updated by google because of security issue.

No its not a permission issue. if your trying accessing this path mnt/sdcard/ its restricted now. If your app hasn't opted out of scoped storage and requests the READ_EXTERNAL_STORAGE permission.

refer this link https://developer.android.com/about/versions/11/privacy/storage

solution 1 - Try to use this file access path is Android/data/data/packageName/

I mean if your using this logic to access file - Environment.getExternalStorageDirectory() instead of above try this - Context.getExternalFilesDir(null)

solution 2 - if your managing media files then try to refer this link https://source.android.com/devices/media/media-provider

I hopes it will help you...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SaranSchwifty