原因:
Android N对访问文件权限收回,按照Android N的要求,若要在应用间共享文件,您应发送一项 content://URI,并授予 URI 临时访问权限。
而进行此授权的最简单方式是使用 FileProvider类。
解决方案:
1.在manifest文件中注册一个provider
<application>
......
<provider
android:authorities="你的应用名.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>
</application>
ps:1.android:authorities属性必须全局一样,例如主工程和library功能中必须是同一个字符串;(编译可以通过,运行会崩溃)
2.android:name 属性是不能一致的,必须使用不同包名下的FileProvider,继承FileProvider;(不然编译不过去)
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-cache-path
name="external_cache_path"
path="" />
</paths>
path 代表要共享的目录
name 只是一个标示,随便取吧 自己看的懂就ok
例如:共享这个目录
//mContext.getExternalCacheDir()+"/aaa.png"
则:name随便;path=" ";path为空串就可以了;
//mContext.getExternalCacheDir()+"/myfile/aaa.png"
则:name随便;path="myfile";
3.代码:
/**
* 开启相机
*/
public void openCamera() {
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(mContext.getExternalCacheDir(), getPhotoFileName());
//mContext.getExternalCacheDir()+"/myfile/aaa.png"
photoFileName = file.getAbsolutePath();
Uri uri;
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(mContext.getApplicationContext(), "com.xmen.xteam.fileprovider", file);
} else {
uri = Uri.fromFile(file);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
((Activity)mContext).startActivityForResult(intent,PictureAdapter.REQUEST_CODE);
}