I'm trying to get image from device gallery and then show it in another activity. 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 activity I get SecurityException
. Docs says permission for received uri lasts until the activity that receives them is finished. So when my activity is finished I have no permission to read file.
Is there some way to prolongate uri permission to allow other activities open file by this uri? Or only way is to copy image to local app storage?
UPD: My goal is saving received uri in local db and allowing other activities and services read this file.
Is there some way to prolongate uri permission to allow other activities open file by this uri?
Yes. Include FLAG_GRANT_READ_URI_PERMISSION
when you pass the Uri
from the first activity to the second activity:
startActivity(new Intent(this, Something.class).setData(uri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With