Search code examples
javaandroidandroid-studioandroid-gallery

java.lang.SecurityException:


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);
}

Solution

  • you need to add

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

    under the <manifest> tag in your manifest file