引起原因:android.os.FileUriExposedException:执行所需步骤后

Getting Caused by: android.os.FileUriExposedException: after performing required steps

我关注了几篇关于 FileUriExposedException 问题及其解决方法的文章。我关注了其中一个,并进行了关注,但毕竟我仍然收到以下错误消息。

Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/My_photo_1518346135450 exposed beyond app through ClipData.Item.getUri()

这是我目前所做的

AndroidManifest.xml

   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>

    </provider>

provider_paths.xml

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

这就是我在 activity 中所做的,然后再启动相机。

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    randomString= String.valueOf(System.currentTimeMillis());
    FILE_NAME= FILE_NAME+randomString;
    File photo = new File(Environment.getExternalStorageDirectory(), FILE_NAME);


    String authorities = getApplicationContext().getPackageName()+ ".fileprovider";
    Uri imageURI= FileProvider.getUriForFile(this, authorities, photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,imageURI);
    startActivityForResult(intent, REQUEST_CODE);

startActivityForResult 之前使用它,您必须授予读取 URI 的标志..

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);