-1

I have made an application that opens my gallery when I click a button, when I select an image I want it to display on my screen however when I select a photo I want I get a runtime saying

 Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/59 from pid=9364, uid=10055 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

My Code is as fallows

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            img.setImageURI(selectedImageUri);
        }
    }
}


public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52

3 Answers3

2

you need to add

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

under the <manifest> tag in your manifest file

spectacularbob
  • 3,080
  • 2
  • 20
  • 41
1

Is this happening on android 6.0 as well? Then you will have to go for runtime permissions as well. Pre marshmallow-

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> in the android manifest file.

Marshmallow and above(Targeted SDK)- You should be checking if the user has granted permission of external storage by using:

    public  boolean haveStoragePermissions() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

Then make your activity implement OnRequestPermissionResult-

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    }
}
Vaibhav Sharma
  • 2,293
  • 1
  • 17
  • 24
0

You have to add the permission in manifest file add below line in manifest tag manifest

Haroon
  • 497
  • 4
  • 13