安卓异常:android.os.FileUriExposedException:

    android.os.FileUriExposedException: file:///storage/emulated/0/picture1537259111.jpg exposed beyond app throughClipData.Item.getUri()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
        at android.net.Uri.checkFileUriExposed(Uri.java:2346)
        at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:8909)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:8894)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
        at android.app.Activity.startActivityForResult(Activity.java:4224)
        at android.app.Activity.startActivityForResult(Activity.java:4183)
        at com.zxyoyo.apk.xunfei.voicedemo.faceonline.OnlineFaceDemo.onClick(OnlineFaceDemo.java:393)
        at android.view.View.performClick(View.java:5610)
        at android.view.View$PerformClick.run(View.java:22265)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

起因:打开相机打算拍照

解决:编写file-provider

authorities:自己的包名+随便一个名称(整个字符串代码中会调用)

resource:下一步需要新建的一个文件

<provider
            android:authorities="com.zxyoyo.apk.xunfei.fileprovider"
            android:name="android.support.v4.content.FileProvider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepath"/>
        </provider>

res目录下新建xml文件夹(存在的话就不用了)

新建filepath文件

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

    <files-path name="my_img" path="img/" />
    <external-path name="img" path="img/" />
    // 说明:name的对应关系 path为文件夹的名称
    files-path          ------>Context.getFilesDir()
    cache-path          ------>Context.getCacheDir()
    external-path       ------>Environment.getExternalStorageDirectory()
    external-files-path ------>Context.getExternalFilesDir()
    external-cache-path ------>Context.getExternalCacheDir()
</paths>

java代码调用:

Uri uri;
mPictureFile = new File(Environment.getExternalStorageDirectory(),
                        "/img/picture" + System.currentTimeMillis()/1000 + ".jpg");
System.out.println(mPictureFile.getAbsolutePath()+"--file path");
if (Build.VERSION.SDK_INT >= 24) {
      uri =FileProvider.getUriForFile(this.getApplicationContext(),"com.zxyoyo.apk.xunfei.fileprovider", mPictureFile);
                }else {
                    uri = Uri.fromFile(mPictureFile);
                }

猜你喜欢

转载自blog.csdn.net/qwe1314225/article/details/82759631