关于Android7.0及以上版本FileUriExposedException的问题
2017-11-10 19:04
441 查看
在开发读取本地图片的功能时,使用了Uri.fromFile(path);
在7.0以下版本是没有问题的,但是在7.0及以上版本则会报出FileUriExposedException,这是Google为了安全考虑,做出的调整,同时也给出了对应的解决办法。
办法一:使用FileProvider
在清单文件中的application节点下添加
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="xxx.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
其中xxx最好用包名来标识,在file_paths.xml中编写该Provider对外提供文件的目录。
使用时:打开照相机
File file = new File(path);
// 第二个参数是manifest中定义的`authorities`
Uri uri = FileProvider.getUriForFile(context, "xxx.fileprovider", file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
...... 其他参数
intent.putExtra(MediaStore.EXTRA_OUTPUT,
uri);
//给目标一个临时的授权。
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
办法二:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, TAKE_PHOTO_REQ_CODE);
在7.0以下版本是没有问题的,但是在7.0及以上版本则会报出FileUriExposedException,这是Google为了安全考虑,做出的调整,同时也给出了对应的解决办法。
办法一:使用FileProvider
在清单文件中的application节点下添加
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="xxx.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
其中xxx最好用包名来标识,在file_paths.xml中编写该Provider对外提供文件的目录。
使用时:打开照相机
File file = new File(path);
// 第二个参数是manifest中定义的`authorities`
Uri uri = FileProvider.getUriForFile(context, "xxx.fileprovider", file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
...... 其他参数
intent.putExtra(MediaStore.EXTRA_OUTPUT,
uri);
//给目标一个临时的授权。
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
办法二:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, TAKE_PHOTO_REQ_CODE);
相关文章推荐
- Android N及以上使用Uri的报FileUriExposedException问题
- 解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException
- Android的SDK版本N以上报错:android.os.FileUriExposedException
- Android7.0文件共享问题(FileUriExposedException)
- Android N 版本迭代/应用内升级 FileUriExposedException问题处理
- 解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException
- Android7.0及以上android.os.FileUriExposedException,解析安装包出现异常
- 关于note2等(Android4.1版本)以上无法启动支付宝的问题"java.security.spec.InvalidKeySpecException"
- 关于note2等(Android4.1版本)以上无法启动支付宝的问题"java.security.spec.InvalidKeySpecException"
- Android7.0升级安装APK,FileUriExposedException问题
- 关于Android7.0版本及其以上系统获取本地数据(图片和文件)报系统出现异常,正在退出问题
- 安卓学习笔记(16)-关于File(path).listFiles()在android7.0及以上版本运行返回null的问题【实测有效】
- 关于note2等(Android4.1版本)以上无法启动支付宝的问题"java.security.spec.InvalidKeySpecException"
- 关于Android 6.0及以上版本用PendingIntent传送数据丢失问题
- 解决Android7.0以上版本升级apk时报android.os.FileUriExposedException的问题
- 关于JBOSS4.2以上版本监听地址的问题
- Android7.0解决 android.os.FileUriExposedException: file:///storage/emulated/0/
- 关于Bugly版本兼容Android7.0 FileProvider与PhotoPicker冲突问题
- Android7.0 FileUriExposedException
- 关于Android6.0(23以上)版本权限管理的问题