Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission Denial: requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() (API 23)

I am trying to request user permissions at runtime. The API is 23 and I want to pick up an image from the phone's gallery. Following some snippets, this is the code I have so far:

In the onCreate() of the Activity I check:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
            PackageManager.PERMISSION_GRANTED) {
        imageUploader5.setEnabled(true);
        ActivityCompat.requestPermissions(this, new String[]
                { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
    }

Then I override:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 0) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            imageUploader5.setEnabled(true);
        }
    }
}

But I still cannot make the app to run on the AVD.

EDIT: Permissions in the manifest:

<uses-permission-sdk-23 android:name="android.permission.CAMERA" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission-sdk-23 android:name="android.permission.READ_EXTERNAL_STORAGE" />
like image 725
user6456773 Avatar asked Dec 10 '22 14:12

user6456773


2 Answers

By how you structured your if statement, you will ask for user permissions only if they are already granted. Add an else block as follows:

// Enable if permission granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
        PackageManager.PERMISSION_GRANTED) {
    imageUploader5.setEnabled(true);
} 
// Else ask for permission
else {
    ActivityCompat.requestPermissions(this, new String[]
            { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}

Edit

Generalize your user-permission in the manifest (without the -sdk-23 suffix) so it can be used by different and future API levels:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
like image 61
Matei Radu Avatar answered May 08 '23 15:05

Matei Radu


defaultConfig {
    applicationId "com.myapp"
    minSdkVersion 11
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}

will just do it. note the targetSdkVersion 22

like image 24
android_griezmann Avatar answered May 08 '23 16:05

android_griezmann