'Permission Denied Issue in Android 11(R)

Now we are migarating Android 11(R) for our testing purpose, but we are facing "Permission denied" issue.

Path: /mnt/sdcard/Android/data/package_name/files/package_name/*

Note: Get the public path using below code,

Context context = _activity.getApplicationContext();
return context.getExternalFilesDir(null) +"/" + getPackageName();

We need to do anything special for Android 11(R)?

Could you please suggest?



Solution 1:[1]

@VMS android has made scoped storage mandatory from android 11 on words. To migrate to scoped storage use ApplicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath();

we can not pass null into getExternalFilesDir();

Solution 2:[2]

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

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

I mean if your using this logic to access file - try to use this Context.getExternalFilesDir(null)

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

I hopes it will help you...

Solution 3:[3]

Add requestLegacyExternalStorage in your AndroidManifest.xml inside <Application :

<application
    android:name=".Application"
    android:allowBackup="true"
    android:appComponentFactory="whateverString"
    android:icon="@drawable/icon"
    android:requestLegacyExternalStorage="true"

Solution 4:[4]

you most get external storage permissions

manifest :

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

onCreate() method:

int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_MEDIA);
} else {
    readDataExternal();
}

override onRequestPermissionsResult method to get callback:

 @Override
 public void onRequestPermissionsResult(int requestCode, String permissions[],    int[] grantResults) {
 switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_READ_MEDIA:
        if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
            readDataExternal();
        }
        break;

    default:
        break;
}
}

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 Akshat sharma
Solution 2 SaranSchwifty
Solution 3 Mahesh Sehajpal
Solution 4 Farzad Kamali