Android N 使用FileProvider避免android.os.FileUriExposedException
来源:互联网 发布:@Requestbody解析json 编辑:程序博客网 时间:2024/05/19 13:13
在Android 7.0及以上版本上,通过以下方式调用相机会报错android.os.FileUriExposedException
imageUri = Uri.fromFile(mTmpImgFile);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);mContext.startActivityForResult(intent, CODE_CHOOSE_FROM_CAMERA);
出于安全的考虑,Google推荐采用FileProvider的方式来代替。
替换FileProvider只需要三步即可:
1. 在res/xml/中指定可用文件
<!-- res/xml/filepaths.xml --><paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="my_images" path="images/"/> <files-path name="my_docs" path="docs/"/></paths>
节点必须包含有最少一个以下子节点:
2. 在AndroidManifest.xml中添加如下provider
<application android:name=".GlobalApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher1" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> ....... <provider android:name="android.support.v4.content.FileProvider" android:authorities="yorek.com.solitaire.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths"/> </provider> ...... </application>
meta-data中android:name固定为android.support.FILE_PROVIDER_PATHS,android:resource则为第一步中创建的xml文件
3. 在代码中使用
在代码中使用时,要注意判断正在运行设备的版本号
private void chooseFromCameraInternal() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri imageUri; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) { imageUri = Uri.fromFile(mTmpImgFile); } else { // support for N imageUri = FileProvider.getUriForFile(mContext, AUTHORITY, mTmpImgFile); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); mContext.startActivityForResult(intent, CODE_CHOOSE_FROM_CAMERA);}
0 0
- Android N 使用FileProvider避免android.os.FileUriExposedException
- 【随心笔录】Android N 文件Uri传递,报出FileUriExposedException异常,使用FileProvider
- Android N android.os.FileUriExposedException 注意事项
- android-解决 Android N 上 报错:android.os.FileUriExposedException
- android-解决 Android N 上 报错:android.os.FileUriExposedException
- android.os.FileUriExposedException 解决方法
- android.os.FileUriExposedException
- android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- 解决 Android N 上 报错:android.os.FileUriExposedException
- Android N共享文件报错:android.os.FileUriExposedException
- 解决 Android N 上 报错:android.os.FileUriExposedException
- 解决 Android N 上 报错:android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- (转)Android N安装apk报错:android.os.FileUriExposedException
- Android N FileUriExposedException异常
- C/C++——模版相关知识
- 错误积累
- 柏林纪行(中):Node.js_Collaboration_Summit
- eclipse中svn的各种图标详解
- 在Unity中Debug.log和Print的区别
- Android N 使用FileProvider避免android.os.FileUriExposedException
- Android Modle中引用xxx.aar文件后项目引用当前Modle后报找不到xxx.aar问题
- SSM项目-医药采购-03 根据条件查询用户信息
- lua中判断table中是否包含某个值
- [占坑]Tinkoff Challenge
- SSH原理与运用(一):远程登录
- 编程基础题-4
- Unity keystore的小问题
- sql 语句大全