Permission to delete image from gallery is lost after restart

My application allows the user to view some selected images from the gallery or other places. I request Uri images via:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, PickerFragment.PICK_PHOTO);

And then get Uri in onActivityResult (int requestCode, int resultCode, Intent data) and access these images to put them in my applications via:

Uri imageUri= data.getData();

When the application ends, I save all the Uri in my database as strings:

String imageUriString = imageUri.toString();
saveToDB (imageUriString);

However, when I restart the application and restore all Uri from my database with:

String imageUriString = restoreFromDB ();
imageUri = Uri.parse (imageUriString);

the app no ​​longer has rights for these Uri in Android 7.1. I view images through:

ContentResolver cr = mContext.getContentResolver();
InputStream is = cr.openInputStream(imageUri);
BitmapFactory.decodeStream(is, null, o);

But get this exception:

java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.photos.contentprovider.impl.MediaContentProvider from ProcessRecord

? , .

, , .

PS , Uri - , . , , .

PPS , Camera and External Storage, .

+1
2

, @greenapps , :

  • :

    Intent photoPickerIntent = Intent (Intent.ACTION_OPEN_DOCUMENT);

  • in onActivityResult() Uri, :

    Uri selectedImage = data.getData();

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) { getContentResolver(). takePersistableUriPermission (selectedImage, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);           }

+1

, :

 ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    1);

, :

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {

          // If request is cancelled, the result arrays are empty.
          if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.          
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

: https://developer.android.com/training/permissions/requesting.html

0

Source: https://habr.com/ru/post/1686934/


All Articles