Android 7.0中拍照和图片裁剪适配的问题详解
作者:LeBron_Six 时间:2022-09-27 07:29:57
前言
Android 7.0系统发布后,拿到能升级的nexus 6P,就开始了7.0的适配。发现在Android 7.0以上,在相机拍照和图片裁剪上,可能会碰到以下一些错误:
Process: com.yuyh.imgsel, PID: 22995
// 错误1
android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.yuyh.imgsel/cache/1486438962645.jpg exposed beyond app through ClipData.Item.getUri()
// 错误2
android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/RxGalleryFinal/IMG_20161018180127.jpg exposed beyond app through Intent.getData()
主要是由于在Android 7.0以后,用了Content Uri 替换了原本的File Uri,故在targetSdkVersion=24
的时候,部分 “`Uri.fromFile()
“` 方法就不适用了。 **File Uri 与 Content Uri 的区别** - File Uri 对应的是文件本身的存储路径 - Content Uri 对应的是文件在Content Provider的路径 所以在android 7.0 以上,我们就需要将File Uri转换为 Content Uri。
具体转换方法如下:
/**
* 转换 content:// uri
*
* @param imageFile
* @return
*/
public Uri getImageContentUri(File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
那么,我们在裁剪的时候,应该如下调用:
private void crop(String imagePath) {
File file = new File("xxx.jpg");
cropImagePath = file.getAbsolutePath();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(getImageContentUri(new File(imagePath)), "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", config.aspectX);
intent.putExtra("aspectY", config.aspectY);
intent.putExtra("outputX", config.outputX);
intent.putExtra("outputY", config.outputY);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, IMAGE_CROP_CODE);
}
这样就解决了裁剪的问题,但是!!拍照的时候就会出现以下错误:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
tempFile = new File(FileUtils.createRootPath(getActivity()) + "/" + System.currentTimeMillis() + ".jpg");
LogUtils.e(tempFile.getAbsolutePath());
FileUtils.createFile(tempFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
startActivityForResult(cameraIntent, REQUEST_CAMERA);
}
android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.yuyh.imgsel/cache/1486438962645.jpg exposed beyond app through ClipData.Item.getUri()
这是因为拍照存储的文件,也需要以Content Uri的形式,故采用以下办法解决:
Step.1
修改AndroidManifest.xml
<application
...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="{替换为你的包名}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
Step.2
在res/xml/下新建provider_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Step.3
修改拍照时的参数
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getActivity(),BuildConfig.APPLICATION_ID + ".provider", tempFile)); //Uri.fromFile(tempFile)
总结
好了,以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流。
来源:http://blog.csdn.net/yyh352091626/article/details/54908624
标签:android,拍照,裁剪
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java编程常用技术(推荐)
2023-11-06 09:03:44
![](https://img.aspxhome.com/file/2023/1/132081_0s.png)
通过Html网页调用本地安卓(android)app程序代码
2022-05-10 10:14:34
SpringBoot项目中遇到的BUG问题及解决方法
2022-01-19 14:44:38
![](https://img.aspxhome.com/file/2023/7/63287_0s.png)
C#使用log4net记录日志的方法步骤
2021-07-07 18:14:55
![](https://img.aspxhome.com/file/2023/9/106449_0s.png)
Java通过在主循环中判断Boolean来停止线程的方法示例
2022-07-18 09:15:03
Android添加用户组及自定义App权限的方法
2022-09-15 20:08:53
![](https://img.aspxhome.com/file/2023/8/117118_0s.jpg)
Android开发之imageView图片按比例缩放的实现方法
2023-01-09 21:52:51
Java用户登录验证代码
2022-05-11 16:45:46
Java如何基于ProcessBuilder类调用外部程序
2023-11-27 20:19:57
![](https://img.aspxhome.com/file/2023/0/59210_0s.png)
java GUI实现学生图书管理简单实例
2023-11-11 05:00:05
![](https://img.aspxhome.com/file/2023/9/58999_0s.png)
java中List接口与实现类介绍
2022-11-17 02:54:40
DrawerLayout的简单使用及侧滑菜单实现详解
2023-04-27 04:10:59
javax.mail.SendFailedException: Sending failed问题原因
2021-08-07 20:00:57
Android模拟器对应的电脑快捷键说明
2022-11-09 17:44:07
mybatis一直加载xml,找到错误的解决方案
2022-08-12 14:55:26
Android实现仿360桌面悬浮清理内存
2021-08-25 11:21:05
Android第三方控件PhotoView使用方法详解
2021-12-07 20:35:48
![](https://img.aspxhome.com/file/2023/2/105982_0s.gif)
Java编程实现打印螺旋矩阵实例代码
2021-10-16 19:15:14
![](https://img.aspxhome.com/file/2023/2/84202_0s.gif)
Java面试题之基本语法(图解)
2023-11-24 09:37:34
![](https://img.aspxhome.com/file/2023/5/59915_0s.png)
springboot省去配置Tomcat的步骤问题
2023-03-13 18:58:44
![](https://img.aspxhome.com/file/2023/2/98112_0s.png)