在Android 7.0 以上 对图片进行裁剪适配的报错
来源:互联网 发布:出国留学gpa算法 编辑:程序博客网 时间:2024/06/07 04:55
报错 :
android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/GalleryFinal/IMG20170720094945.jpg exposed beyond app through ClipData.Item.getUri()
主要是由于在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
具体解决 有两种方法
方法 一 : 出自github ; 我就不做赘述了 .
方法 二 : 我自己使用的是这种方法
step 1 在AndroidManifest. xml中添加如下代码
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" ... <application ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider> </application></manifest>
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(context,getApplicationContext().getPackageName()
+ ".provider", tempFile)); //Uri.fromFile(tempFile)
+ ".provider", tempFile)); //Uri.fromFile(tempFile)
备注 考虑版本兼容可以如下操作
int currentapiVersion = Build.VERSION.SDK_INT;if (currentapiVersion < 24){ captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); }else{ //解决Android 7.0 相机问题 captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName()+ ".provider", tempFile));}
操作完成 !
阅读全文
0 0
- 在Android 7.0 以上 对图片进行裁剪适配的报错
- android 对图片进行裁剪的方法
- android 7.0以上裁剪和选择sd卡外面的图片适配(日记)
- [Swift3] 对图片进行裁剪
- 对上传的图片进行裁剪处理后保存
- android选择图片进行裁剪
- 解决Android在对Sdcard进行文件的读写操作的时候报错FileNotFoundException
- 调用系统摄像头拍照,对拍照后的图片进行裁剪和压缩处理,并显示在imageview上面
- android图片的裁剪
- android图片的裁剪
- Android图片的裁剪
- Android图片的裁剪
- Android图片裁剪----移动、缩放图片进行裁剪
- android调用系统相机、调用系统相册,调用系统图片裁剪报错的解决方案
- android 4.4以上系统文档和图库裁剪图片加载异常的处理
- Android拍照及图片裁剪、调用系统相册(兼容6.0以上权限处理及7.0以上文件管理)
- 上传图片,对图片进行等比例缩放,及局部裁剪的工具类
- Android CustomShapeImageView对图片进行各种样式裁剪:圆形、星形、心形、花瓣形等
- 一题多解 —— 同时找到序列的最大值最小值
- Stars(数星星)
- 【A ByteOf Python2.7】第5章 运算符与表达式
- Gitlab配置SSH连接
- Super Jumping! Jumping! Jumping!DP
- 在Android 7.0 以上 对图片进行裁剪适配的报错
- Python3 基本数据类型
- HttpCilent(get请求)
- 大致题解
- Android Studio 提示PSI and index do not match错误
- 【A ByteOf Python2.7】第6章 控制流
- 全栈开发工程师,需要哪些知识技能?
- 51Nod 1082 与7无关的数
- 空洞卷积与DeeplabV2实现图像语义分割的测试(tensorflow)