首页 技术 正文
技术 2022年11月21日
0 收藏 381 点赞 2,444 浏览 1099 个字

在开发中,项目里面明明已经添加过拍照或者读取相册的权限,但是在点击拍照或者打开相册的时候应用会崩溃,报一下错误:

Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/com.yzs.nongfeike/20181105100312792.jpg exposed beyond app through ClipData.Item.getUri()

简单查了一下是Android7.0+权限机制改变造成的。网上也有很多解决方法,有的需要在清单文件manifest中定义FileProvider,还需要在res下新建xml文件夹,并新建filepaths.xml.

个人感觉这种比较麻烦,现在贴上我亲自测试有效的解决方法。

Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
min_photo = saveFolder + "/" + ImageUtil.getImageName();
file = new File(min_photo); int currentapiVersion = android.os.Build.VERSION.SDK_INT;
//7.0以下
if (currentapiVersion<24){
intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intentFromCapture, PHOTO);
}else{
//7.0以上
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
Uri uri = this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intentFromCapture, PHOTO);
}

其实很简单,就是做一下手机安卓系统判定,7.0以下的执行原始调用方式。7.0以上的需要使用ContentValues属性,好了,今天就写到这里。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852