Grant Uri permission to other activities

I am trying to get an image from the device gallery and then show it in another action. Code in my activity:

private void startGallery() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(intent, REQ_OPEN_GALLERY) } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQ_OPEN_GALLERY && resultCode == Activity.RESULT_OK) { Uri imageUri = data.getData() // here I save image uri to pass to another activity } } 

When I try to open uri in another action, I get a SecurityException . Docs says that the permission to receive the uri lasts until the activity that receives them is completed. Therefore, when my activity is finished, I am not allowed to read the file.

Is there any way to extend uri permission to allow other actions to open the file using this uri? Or just a way to copy the image to the local application store?

UPD: My goal is to store the resulting uri in local db and allow other actions and services to read this file.

+5
source share
1 answer

Is there any way to extend uri permission to allow other actions to open the file using this uri?

Yes. Include FLAG_GRANT_READ_URI_PERMISSION when you pass the Uri from the first action to the second action:

 startActivity(new Intent(this, Something.class).setData(uri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)); 
+9
source

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


All Articles