Grant Uri permission to another activity

浪子不回头ぞ 提交于 2019-12-21 12:00:45

问题


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.


回答1:


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));


来源:https://stackoverflow.com/questions/39898516/grant-uri-permission-to-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!