您的位置:首页 > 移动开发 > Android开发

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 系统兼容
相关文章推荐