startActivity报错exposed beyond app through Intent.getData()

调用相册打开图片 startActivity时报了个错

startActivityForResult error:file:///storage/emulated/0/Android/data/xxx.jpg exposed beyond app through Intent.getData()

调查发现是AndroidStudio7.0之后官方对这权限做了调整:官方解答:出于用户隐私安全考虑,对于面向 Android 7.0 的应用,Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file://URI。传递软件包网域外的 file://URI 可能给接收器留下无法访问的路径。因此,尝试传递 file://URI会触发 FileUriExposedException。分享私有文件内容的推荐方法是使用 FileProvider。

1.添加在Manifest.xml中

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="xxx.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

 

2. 在新建的资源文件@xml/provider_paths中添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path path="." name="external_storage_root" />
</paths>

下面为文件的子节点

3.URI获取方法

出现该问题时:

File file = new File(filePath);
Uri uri = Uri.fromFile(file);

正确的方法:

Uri contentUri = FileProvider.getUriForFile(context, "xxx.fileprovider", file);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值