FileUriExposedException: file:////XXXXXXXX g exposed beyond app through ClipData.Item.getUri()问题的解决

FileUriExposedException: file:////XXXXXXXX g exposed beyond app through ClipData.Item.getUri()问题的解决

前段时间把App的有关拍照的代码写好了,也适配了安卓7.0,把我们老大的华为的手机搞来玩玩,偷偷给升级到了安卓7.0来测一下,果然出了问题,很是棘手。

FileUriExposedException: file:////XXXXXXXX g exposed beyond app through ClipData.Item.getUri()

这是什么错误,特别是后面的exposed beyond app through ClipData.Item.getUri(),我都没看懂,所以到网上找这是个解决方案
 case ChatBottomView.FROM_CAMERA:// 相机
                        if (!CAN_WRITE_EXTERNAL_STORAGE) {
                            Toast.makeText(ServiceChatActivity.this, "权限未开通\n请到设置中开通相册权限", Toast.LENGTH_SHORT).show();
                        } else {
                            Intent openCameraIntent = new Intent(
                                    MediaStore.ACTION_IMAGE_CAPTURE);
                            camPicPath = PathUtils.getSavePicPath(ServiceChatActivity.this);
                            if (Build.VERSION.SDK_INT < 24){
                                Uri uri = Uri.fromFile(new File(camPicPath));
                                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                                startActivityForResult(openCameraIntent,
                                        ChatBottomView.FROM_CAMERA);
                            }else {
                                //适配Android7.0
                                ContentValues contentValues = new ContentValues( 1 );
                                contentValues.put( MediaStore.Images.Media.DATA,
                                        camPicPath);
                                Uri uri = getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues );
                                grantUriPermission( "com.example.lab.android.nuc.chat",uri,Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
                                openCameraIntent.addFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION );
                                openCameraIntent.addFlags( Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                                openCameraIntent.putExtra( MediaStore.EXTRA_OUTPUT,uri);
                                startActivityForResult( openCameraIntent,ChatBottomView.FROM_CAMERA );
                            }
                        }
                        break;

这一回应该可以了,没错,真的行了,我已开始是写的FileProvider模式的,然后再用Intent传递uri,一直报错,不知道咋地。像这样的

File cameraPhoto = new File(cameraPhotoPath);
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri photoUri = FileProvider.getUriForFile(
                this,
                getPackageName() + ".provider",
                cameraPhoto);
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    startActivityForResult(takePhotoIntent, REQUEST_TAKE_PHOTO);
一直出错,不知道咋地,感觉不爱了!!!

后来在CSDN上找到一个帖子

有需要的小伙伴可以去看看,所以我在此基础上出篇博客,收录一下。
好了,mark这里,谁有需要直接拷代码就行了,还有记得在调起相机前申请存储权限,和调用相机权限
(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA)

你可能感兴趣的:(android)