解决android.os.FileUriExposedException,exposed beyond app through ClipData.Item.getUri() 异常

最近公司的项目在Android 7.0的测试机中出现以下的异常:

# main(1)
android.os.FileUriExposedException
file:///storage/emulated/0/xiangmu/3462884.jpg exposed beyond app through ClipData.Item.getUri()

android.os.StrictMode.onFileUriExposed(StrictMode.java:1816)
android.net.Uri.checkFileUriExposed(Uri.java:2350)
android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
android.content.Intent.prepareToLeaveProcess(Intent.java:9052)
android.content.Intent.prepareToLeaveProcess(Intent.java:9037)
android.app.Instrumentation.execStartActivity(Instrumentation.java:1530)
android.app.Activity.startActivityForResult(Activity.java:4391)
android.app.Activity.startActivityForResult(Activity.java:4335)
com.i8dayou.an.zb.push.TCPublishSettingActivity.a(TCPublishSettingActivity.java:343)
com.i8dayou.an.zb.push.TCPublishSettingActivity.c(TCPublishSettingActivity.java:55)
com.i8dayou.an.zb.push.TCPublishSettingActivity$3.onClick(TCPublishSettingActivity.java:306)
android.view.View.performClick(View.java:5646)
android.view.View$PerformClick.run(View.java:22459)
android.os.Handler.handleCallback(Handler.java:761)
android.os.Handler.dispatchMessage(Handler.java:98)
android.os.Looper.loop(Looper.java:156)
android.app.ActivityThread.main(ActivityThread.java:6523)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

解决方法如下:

1.在相对应的页面中,写如下的方法:

private void initPhotoError(){
        // android 7.0系统解决拍照的问题
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
    }

2.在onCreate中调用上述的方法。

以下是个人公众号(longxuanzhigu),之后发布的文章会同步到该公众号,方便交流学习Android知识及分享个人爱好文章:
解决android.os.FileUriExposedException,exposed beyond app through ClipData.Item.getUri() 异常_第1张图片

你可能感兴趣的:(bug处理)