摘自郭霖 《第一行代码》
从Android 7.0系统开始,直接使用本地真实路径的Uri,被认为是不安全的,会抛出一个FileUriExposedException异常,,而FileProvider则是一种特殊的内容提供器,它使用了和内容提供器类似的机制来对数据进行保护,可以选择行地将封装过的Uri共享给外部,从而提高了应用的安全性。
// 创建File对象,用于存储拍照后的图片
File outputImage = new File(getExternalCacheDir(),
"output_image.jpg");
//判断当前Android版本号是否大于等于24
if (Build.VERSION.SDK_INT >= 24){
//如果是则使用FileProvider
imageUri = FileProvider.getUriForFile(MainActivity.this,
"com.example.cameraalbum.fileprovider", outputImage);
} else {
//否则,使用原来的fromFile()
imageUri = Uri.fromFile(outputImage);
}
在AndroidManifest.xml中对内容提供器进行注册:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.camearalbum.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
其中,android:name 属性的值是固定的,android:authoritirs 属性的值必须要和FileProvider.getUriForFile() 方法中的第二个参数一致。
另外,这里还在标签的内部使用来指定Uri的共享路径,并引用了一个@xml/file_paths资源。
在res目录下新建一个xml目录然后新建一个file_paths.xml文件,并修改文件中的内容:
<?xml version="1.0" encoding = "utf-8"?>
<paths xmlns:app="http://schemas.android.com/apk/res/android">
<external-path name = "my_images" path = "" />
</paths>
其中,external-path就是用来指定Uri共享的,name属性的值可以随便填,path属性的值表示共享的具体路径。这里设定空就表示将整个SD卡进行共享,也可以仅共享存放output_image.jpg 这张图片的路径。
还有一点需要注意,在Android 4.4系统之前,访问SD卡的应用关联目录也是要声明权限的,从4.4系统开始不在需要权限声明。
为了兼容老版本系统,还需要在AndroidManifest.xml 中声明一下SD卡的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
结束。
另外还发现一种解决方法,顺便记录下来:
File file = new File(imageFile)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri mImageUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果是7.0android系统
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA,file.getAbsolutePath());
mImageUri= getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
}else{
mImageUri = Uri.fromFile(file);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(intent, REQUEST_PICK_FROM_CAMERA);