Android 7.0调用系统相机报错Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/rpms/14
2017-03-23 16:40
786 查看
原因:
Android N对访问文件权限收回,按照Android N的要求,若要在应用间共享文件,您应发送一项 content://URI,并授予 URI 临时访问权限。
而进行此授权的最简单方式是使用 FileProvider类。解决方法:
1.在manifest中定义FileProvider
<provider android:authorities="com.tdh.rpms.fileprovider" android:name="android.support.v4.content.FileProvider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths"/> </provider>2.在res下新建xml文件夹,并新建filepaths.xml
<?xml version="1.0" encoding="utf-8"?> <paths> <!-- external-path:sd ;path:你的应用保存文件的根目录;name随便定义--> <!-- root-path 手机存储根目录 --> <root-path path="." name="external_storage_root" /> </paths>3.定义两个公共方法,第一个方法为打开系统文件时调用,第二个方法为打开系统相册时调用
/** * 打开文件 * 兼容7.0 * * @param context activity * @param file File * @param contentType 文件类型如:文本(text/html) * 当手机中没有一个app可以打开file时会抛ActivityNotFoundException */ public static void startActionFile(Context context, File file, String contentType) throws ActivityNotFoundException { if (context == null) { return; } Intent intent = new Intent(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//增加读写权限 intent.setDataAndType(getUriForFile(context, file), contentType); if (!(context instanceof Activity)) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } context.startActivity(intent); }
/** * 打开相机 * 兼容7.0 * * @param activity Activity * @param file File * @param requestCode result requestCode */ public static void startActionCapture(Activity activity, File file, int requestCode) { if (activity == null) { return; } Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, getUriForFile(activity, file)); activity.startActivityForResult(intent, requestCode); } public static Uri getUriForFile(Context context, File file) { if (context == null || file == null) { throw new NullPointerException(); } Uri uri; if (Build.VERSION.SDK_INT >= 24) { uri = FileProvider.getUriForFile(context.getApplicationContext(), "com.tdh.rpms.fileprovider", file); } else { uri = Uri.fromFile(file); } return uri; }注意
请注意fileprovider前面为项目包名,并且这个和manifest定义的authorities的内容一致,好了,准备工作做好,接下来就是如何在项目中调用了:
由于我的项目只是用到了调用相机,故只展示打开相机使用方法
String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { File file = new File(fileUtils.getSDPATH(), String.valueOf(System.currentTimeMillis())+ ".jpg"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); return; } } path = file.getAbsolutePath(); FileUtils.startActionCapture(this,file,PHOTOHRAPH); } else { CommonUtil.getToast(context, "没装SD卡"); }
其中FileUtils为我放以上两个公共方法的类,好了,大致就这些了。
相关文章推荐
- Android 7.0调用系统相机(文件访问crash android.os.FileUriExposedException)
- Android 7.0 调取系统相机崩溃解决android.os.FileUriExposedException
- android 7.0系统打开本地文件的问题android.os.FileUriExposedException:file:///storage/emulated/0/
- android 7.0系统解决拍照的问题android.os.FileUriExposedException:file:///storage/emulated/0/test.txt
- Android 7.0 系统相机崩溃解决android.os.FileUriExposedException
- 【适配整理】Android 7.0 调取系统相机崩溃解决android.os.FileUriExposedException
- Android 7.0 FileUriExposedException 解决(调用系统相机)
- 解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException
- 【适配整理】Android 7.0 调取系统相机崩溃解决android.os.FileUriExposedException
- 解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException
- android 7.0系统解决拍照的问题android.os.FileUriExposedException:
- Android 7.0调用相机拍照 报错 FileUriExposedException
- 开发中碰到的问题:Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException: file:///storage/emulated/0/And
- Android 7.0 调取相机崩溃 android.os.FileUriExposedException
- android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/Camera/2018012Android 7.0相继崩溃的问题
- android 7.0 + 相机拍照 FileUriExposedException
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- android 7.0 + 相机拍照 FileUriExposedException
- Android 7.0配置fileprovider共享文件 解决FileUriExposedException