FileProvider
1. 在manifest文件中配置FileProvider路径。
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="name,authorities,exported,grantUriPermissions">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
tools:replace="name,resource"/>
</provider>
tools:replace 是 合并xml 时,替换相应属性。
provider_paths:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-cache-path name="my_images" path="images" />
</paths>
2. 用android 25 上 用 FileProvider.getUriForFile 生成uri, 以下用Uri.parse生成
Uri photoUri;
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.M){
File file;
file = new File(mCachePic);
photoUri = FileProvider.getUriForFile(
act,
act.getPackageName() + ".provider",
file);
}else{
photoUri = Uri.parse(mCachePic);
}
3. 添加权限
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4. crop时, outputUri 要用Uri.parse(path);
Intent intent = new Intent("com.android.camera.action.CROP");
// 照片URL地址
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("outputX", w);
intent.putExtra("outputY", h);
intent.putExtra("scale", true);//黑边
intent.putExtra("scaleUpIfNeeded", true);//黑边
// 输出路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);