FileUriExposedException错误原因
对于面向 Android N 的应用,Android 框架执行的 StrictMode API 政策禁止向您的应用外公开 file:// URI。 如果一项包含文件 URI 的 Intent 离开您的应用,应用失败,并出现 FileUriExposedException 异常。
若要在应用间共享文件,您应发送一项 content:// URI,并授予 URI 临时访问权限。 进行此授权的最简单方式是使用 FileProvider 类。 如需有关权限和共享文件的更多信息,请参阅共享文件。
以图片裁剪作为示例的解决办法
AndroidManifest.xml application标签中添加
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="dream.go.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
provider_paths.xml 位于res/xml文件夹中
详细解释参见FileProvider 类
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="images" path="."/>
</paths>
BaseActivity.java
核心是将file uri的获取方式由fromfile改变为由FileProvider.getUriForFile获取,并对该欲发送的uri授予读写权限,并将接收该uri的目的App的PackageName通过grantUriPermission()函数进行设置。
File tempFile = new File(dir.getAbsolutePath(), PHOTO_FILE_NAME);
Uri photoURI = FileProvider.getUriForFile(context, "dream.go.provider", tempFile);
crop(photoURI);
//Android N crop image
public void crop(Uri uri) {
context.grantUriPermission("com.android.camera",uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
//Android N need set permission to uri otherwise system
//camera don't has permission to access file wait crop
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.putExtra("crop", "true");
//The proportion of the crop box is 1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//Crop the output image size
intent.putExtra("outputX", 800);
intent.putExtra("outputY", 800);
//image type
intent.putExtra("outputFormat", "JPEG");
intent.putExtra("noFaceDetection", true);
//true - don't return uri | false - return uri
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
使用图库选择图片时,需要使用getRealPathFromURI函数将uri转换为实际的文件地址,再将文件拷贝出来进行操作,不然会由于文件没有写权限而导致裁剪失败
//file uri to real location in filesystem
public String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
// Source is Dropbox or other similar local file path
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}
补充下Android7.0版本的文件分享代码,Android7.0版本许多第三方App还没有做兼容,所以分享之后其它App没有接收到也不要大惊小怪啦……
Intent intent2 = new Intent(Intent.ACTION_SEND);
Uri uri2;
if (android.os.Build.VERSION.SDK_INT >= 24) {
// only for VERSION android N
showToast("您正在使用Android 7.0版本文件分享Beta版,可能会发生错误,请悉知!");
uri2 = FileProvider.getUriForFile(context, "dream.go.provider", file);
intent2.setDataAndType(uri2,getContentResolver().getType(uri2));
intent2.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent2.putExtra(Intent.EXTRA_SUBJECT, current_file);
intent2.putExtra(Intent.EXTRA_STREAM, uri2);
//Using the grantUriPermission method we need to provide
//the package name of the app we want to grant
// the permission to. However, when sharing, usually
//we don't know what application the sharing destination is
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent2, 0);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
Log.e("sharefile",packageName);
context.grantUriPermission(packageName, uri2, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}else {
uri2 = Uri.fromFile(file);
//can't use getContentResolver().getType(uri2) as type
intent2.setType("*/*");
intent2.putExtra(Intent.EXTRA_SUBJECT, current_file);
intent2.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
}
Log.e("sharefile",uri2.toString());
if (intent2.resolveActivity(getPackageManager()) != null)
//show as a friendly select dialog with TitleName is current_file
startActivity(Intent.createChooser(intent2, current_file));
else
showToast("分享失败");