解决FileUriExposedException

安卓8.0开始不能够再用

Uri.fromFile();获得uri,今天为了适配这个bug,在网上找了很多方案,但是写的都不是很完善,所以把自己今天解决这个bug的流程写下来

一、配置manifest,在manifest里面加入如下代码

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

二、在之前Uri.fromFile()处用如下代码替换

Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    uri = FileProvider.getUriForFile(getApplicationContext(),
            getPackageName()+".fileprovider"/*此处是为了和manifest一致*/, new File(outputFilePath));
} else {
    uri = Uri.fromFile(new File(outputFilePath));
}

三、在res目录xml下添加filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="files" path="photos" />
    <external-path name="files" path="photos" />
    <root-path path="" name="camera_photos" />/*必须,不然会报java.lang.IllegalArgumentException: Failed to find configured root错误*/
</paths>

至此大功告成!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值