向Uri授予其他活动的权限 [英] Grant Uri permission to another activity

查看:71
本文介绍了向Uri授予其他活动的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从设备库中获取图像,然后在另一个活动中显示它. 我的活动中的代码:

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
    }
}

当我尝试在其他活动中打开uri时,得到SecurityException. 文档说,收到的uri的许可持续到活动接收它们完成.因此,当我完成活动后,我无权读取文件.

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.

是否有某种方法可以延长uri权限,以允许此uri打开其他活动文件?还是唯一的方法是将图像复制到本地应用程序存储中?

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::我的目标是将收到的uri保存在本地数据库中,并允许其他活动和服务读取此文件.

UPD: My goal is saving received uri in local db and allowing other activities and services read this file.

推荐答案

是否有某种方法可以延长uri权限,以允许此uri打开其他活动文件?

Is there some way to prolongate uri permission to allow other activities open file by this uri?

是的. 在包含FLAG_GRANT_READ_URI_PERMISSION时包含您将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));

这篇关于向Uri授予其他活动的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆