-2

Hello guys!

I spent 4 hours searching on stack overflow or videos how to fix the error: Caused by: java.lang.SecurityException: Permission Denial [...] requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

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

I try in my application to display a picture from the gallery. Here is the Manifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".AddCountry" android:label="Add Country"></activity>
</application>

The Activity where I try to display the picture

public class AddCountry extends AppCompatActivity implements Constant{

private ImageView imageView;
private Button btnUploadPhotos;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_country);
    initComponents();
}

private void initComponents() {
    imageView=(ImageView) findViewById(R.id.imgView_add_country_activity_uploadphotos);
    btnUploadPhotos=(Button)findViewById(R.id.btn_add_country_activity_uploadphotos);
    btnUploadPhotos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openGallery();
        }


    });

}

private void openGallery() {
    Intent gallery=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery,ADD_COUNTRY_REQUEST_PICK_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK&&requestCode==ADD_COUNTRY_REQUEST_PICK_IMAGE)
    {
        Uri imageUri=data.getData();
        String[] filePathColumn={MediaStore.Images.Media.DATA};
        Cursor cursor=getContentResolver().query(imageUri,filePathColumn,null,null,null);
        cursor.moveToFirst();

        int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
        String picturePath=cursor.getString(columnIndex);
        cursor.close();

        Bitmap bitmap=BitmapFactory.decodeFile(picturePath);
        Drawable drawable=new BitmapDrawable(bitmap);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

First I tried in onActivityResult like this:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK&&requestCode==ADD_COUNTRY_REQUEST_PICK_IMAGE)
    {
        Uri imageUri=data.getData();
        imageView.setImageURI(imageUri);
    }
}

I got no errors but nothing happened when I picked a image.

The app bundle:

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
    applicationId "com.example.rares.proiect"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
}

UPDATE

I fixed the problem adding this:

if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
        Manifest.permission.READ_EXTERNAL_STORAGE)) {
    // Explain to the user why we need to read the contacts
}

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant

return;
ProgIsMetal
  • 462
  • 2
  • 5
  • 18

1 Answers1

0

Since you are targeting sdk >= 23, you need to take a look into runtime permissions

Alexander Perfilyev
  • 6,739
  • 2
  • 17
  • 30