安卓学习笔记---调用相机功能在安卓7.0以上报错android.os.FileUriExposedException
来源:互联网 发布:淘宝怎么去同款 编辑:程序博客网 时间:2024/05/22 07:50
解决方法:
1.
在AndroidManifest.xml中添加如下代码
<!--解决在安卓7.0以上调用相机拍照出现android.os.FileUriExposedException 问题--><!--注意: authorities:app的包名.fileProvider grantUriPermissions:必须是true,表示授予 URI 临时访问权限 exported:必须是false resource:中的@xml/file_paths是我们接下来要添加的文件--><provider android:name="android.support.v4.content.FileProvider" android:authorities="app的包名.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /></provider>
2.
在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件,里面内容如下
path:需要临时授权访问的路径(.代表所有路径)
name:就是你给这个访问路径起个名字
<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path path="." name="external_storage_root" /></paths>
3.在需要打开相机时进行判断
//获取权限,打开照相机private void openCamera() { Uri imageUri; //创建File对象,用于存储拍摄的照片 File outputImage = new File(getPhotoPath()); try { if (outputImage.exists()){ outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ imageUri = FileProvider.getUriForFile(UserImageUpdateActivity.this,"app包名.fileprovider",outputImage); }else { imageUri = Uri.fromFile(outputImage); } //启动相机 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, REQUEST_CODE_FROM_CAMEIA);}// 拍照路径public String getPhotoPath() { File file = new File(Constant.HEAD_PORTRAIT); if (!file.exists()) { file.mkdirs(); } String path = file.getAbsolutePath() + "/userphoto.jpg"; return path;}
判断sdk版本,如果大于24,则使用provider,低于则使用应用关联缓存目录存下图片。
阅读全文
0 0
- 安卓学习笔记---调用相机功能在安卓7.0以上报错android.os.FileUriExposedException
- 安卓7.0报错android.os.FileUriExposedException
- 解决android 7.0系统,调用相机报:android.os.FileUriExposedException错
- Android 7.0调用相机拍照 报错 FileUriExposedException
- Android 7.0调用系统相机报错Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/rpms/14
- Android调用安卓相机拍照上传
- 安卓7.0以上版本更新apk报错
- 安卓7.0调用相机引起错误
- 安卓的相机调用
- 安卓调用相机 相册
- 安卓简单调用相机
- Android的SDK版本N以上报错:android.os.FileUriExposedException
- 安卓笔记 自定义相机
- Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException解决办法
- Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException(已修复)
- 安卓笔记android.os.NetworkOnMainThreadException异常
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- Hadoop2.6.0伪分布式集群搭建
- php 垃圾回收深入理解
- 设置电量条颜色
- PyQt5.6 + pycharm 配置 以及 pyinstaller 生成exe 傻瓜教程 100%成功
- HDU-2639 Bone Collector II(01背包k解问题)
- 安卓学习笔记---调用相机功能在安卓7.0以上报错android.os.FileUriExposedException
- Linux的ls命令
- ORA-22835:缓冲区对于CLOB到CHAR转换而言太小
- 十个jQuery代码片段助力Web开发
- FFmpeg的Android端调用库ffmpeg-android的简单使用
- 3 微服务实战系列
- 重新铸就Json
- 7-14 天梯地图(30 分)
- windows下的cmd总结