Android 7.0拍照、选择照片相关错误

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/danfengw/article/details/82588273

1、

s.FileUriExposedException: file:///storage/emulated/0/DCIM/IMG_4146227344282226873.jpg exposed beyond app through ClipData.Item.getUri()

(1)Android manifest中添加一下代码,最好使用${applicationId}

 <!--sdk 24 以上 拍照需要特殊处理-->
        <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>

(2)在values目录下创建xml文件夹,再创建provider_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--"."表示所有路径-->
    <external-path name="com.flyfish.healthmanage_prac.fileprovider" path="."/>

</paths>

(3)调用相机时,做特殊处理

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    FileProvider.getUriForFile(this, AppConfig.getApplicationContext().getPackageName() + ".fileprovider", file));
        } else {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        }

(4)如果还涉及到剪裁图片,也需要做处理, intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

/*
   * 剪切图片
   */
    private void cropImg(Uri uri) {
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框的比例,1:1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);
        intent.putExtra("outputFormat", "JPEG");// 图片格式
        intent.putExtra("noFaceDetection", true);// 取消人脸识别
        intent.putExtra("return-data", true);

        // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT
        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

    @Override
    public void onFailed(int code, String message) {

    }

2、.IllegalArgumentException: Failed to find configured root that contains /storage/6130-6330/DCIM/P71024-164048.jpg
需要在上面的provider_paths.xml中添加root-path这一项。
想要了解更多推荐:https://www.jianshu.com/p/121bbb07cb07

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--"."表示所有路径-->
    <external-path name="com.flyfish.healthmanage_prac.fileprovider" path="."/>
    <root-path
        name="root_path"
        path="." />
</paths>

3、如果你跟我一样使用了MultiImageSelector,在MultiImageSelector设置可以拍照时也会报一个错误,

s.FileUriExposedException: file:///storage/emulated/0/DCIM/IMG_4146227344282226873.jpg exposed beyond app through ClipData.Item.getUri()

(好像是上面这个错误,有点记不清了),可以通过在baseApplication中设置下面几行代码来解决

// android 7.0系统解决拍照的问题
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            builder.detectFileUriExposure();
        }

猜你喜欢

转载自blog.csdn.net/danfengw/article/details/82588273