Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grant Uri permission to another activity

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.

like image 923
nnesterov Avatar asked Oct 06 '16 14:10

nnesterov


1 Answers

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));
like image 167
CommonsWare Avatar answered Sep 29 '22 23:09

CommonsWare