Android 7.0 拍照崩溃 FileUriExposedException

三星 SM G9550  android7.0 拍照崩溃。

崩溃日志:

# main(1)

android.os.FileUriExposedException

file:///storage/emulated/0/task/camera/a499e775-75ac-4d39-9712-5469ec1b1b51 exposed beyond app through ClipData.Item.getUri()


解决方案:

1.manifest里面添加配置:

    android:name="android.support.v4.content.FileProvider"

    android:authorities="${applicationId}.fileprovider"

    android:exported="false"

    android:grantUriPermissions="true">

        android:name="android.support.FILE_PROVIDER_PATHS"

        android:resource="@xml/file_paths" />

${applicationId}需要替换成你的包名

2.在res 下增加xml 包,然后新建file_paths.xml

file_paths内容:

<?xml version="1.0" encoding="utf-8"?>

<paths>

    <external-path name="images" path="task/camera" />

</paths>

3.修改读取uri的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID +

".fileprovider", file);

}else {

contentUri = Uri.fromFile(file);

}

intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);

其中BuildConfig.APPLICATION_ID就是包名,和manifest清单文件中的authorities的applicationId 应该相同。即:

BuildConfig.APPLICATION_ID +".fileprovider"==${applicationId}.fileprovider

如果出现

Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference

说明刚才说的两个值不相等,或者是xml的路径有问题。

path路径对应:

子节点对应路径例子

files-path→ Context.getFilesDir()


cache-path→ Context.getCacheDir()


external-path→ Environment.getExternalStorageDirectory()  →  /storage/emulated/0/


external-files-path→ Context.getExternalFilesDir(null)


external-cache-path →Context.getExternalCacheDir()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,126评论 25 709
  • 只简述我发现问题的根源,有些是适配了7.0,会报权限失败问题,那是由于没有动态授权导致,接下来我一步一步给大家实现...
    Wocus阅读 2,412评论 4 5
  • Android N系列适配---FileProvider Android 7.0的适配,主要包含方面: Andro...
    25a58172fbb5阅读 7,163评论 3 32
  • 有病就去看医生!有病就去看医生!有病就去看医生!重要的事情说三遍! 10.27 番茄籽白天有点咳嗽和流鼻涕,晚上突...
    沙格西矛阅读 405评论 0 0
  • 1 今天看到堂弟的朋友圈,晒了一张到深圳的机票,我很好奇,就在微信上问他,现在去深圳干什么,他回我“现在在老家县城...
    Mr_Zhang2O12阅读 368评论 0 0