java.lang.SecurityException: Permission Denial: writing android.support.v4.content.FileProvider uri content://com.tianshaokai.demo.fileprovider/camera_photos/temp/1480414713257.jpg from pid=23075, uid=10041 requires the provider be exported, or grantUriPermission()
public void cropPhoto(File file) {
cropfile = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
if (!cropfile.getParentFile().exists()) cropfile.getParentFile().mkdirs();
Uri outputUri = FileProvider.getUriForFile(this, getProvider(), cropfile);
Uri imageUri = FileProvider.getUriForFile(this, getProvider(), file);//通过FileProvider创建一个content类型的Uri
Intent intent = new Intent("com.android.camera.action.CROP");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 400);
// intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
// intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, 1002);
}
file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
Uri imageUri = FileProvider.getUriForFile(this, getProvider(), file);//通过FileProvider创建一个content类型的Uri
Log.d(TAG, "imageUri: " + imageUri);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将拍取的照片保存到指定URI
startActivityForResult(intent, 1001);
现在就是拍照完成后,裁剪 就报最上边的 权限错误,测试机是 7.0 的模拟器
看到大家的回答我补充一下啊,我已经添加了6.0 的 SD卡和摄像头 动态权限,我现在的问题是裁剪完 保存时 报上边的错误。
To intercept the output Uri of the photo, you can only use Uri.fromFile, not FileProvider.getUriForFile
Yes, the permissions are wrong. After 6.0, permissions need to be obtained dynamically.
There will be no prompt now, and you need to actively initiate permission application. Generally, for taking pictures, there are two permissions for SD card and camera. Knowledge related to the 6.0 permission model can be queried online. http://www.tuicool.com/articl... I found it online and see if it can be solved,
My current thoughts and practices:
1. Use FileProvider to be compatible with Android N and above
2. Use a file to save the taken and cropped pictures
Steps:
1. Declare FileProvider in AndroidManifest.xml
2. Create the xml directory under res and create filepaths.xml with the following content:
Note: The advantage of saving with this path is that the file will be deleted when the application is uninstalled. file/header/ is a custom path
3. The calling point needs to handle external storage permissions (because the system is used to take pictures, there is no need to provide camera permissions)
4. URI processing, the path has been declared in filepath.xml, you need to define a file name to save the image (save.jpg)
5. Photo processing
6. Cutting processing
Finally, one more thing: some mobile phones may have the resultCode after trimming always be RESULT_CANCEL, so it is recommended to set the launchMode of the Activity using this function to singleTask
Security exception, user authorization is required for version 6.0 and above.