在Android 版本7.0 以上
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
时,应用内提供给其他应用的文件路径不能以file://格式提供,比如应用内自动更新跳转安装页,使用系统相机裁剪功能。
原因:Android系统为了安全隐私。
解决办法 FileProvider + 传递content:// 格式的url。
1.在AndroidManifest.xml 文件的application 标签下添加
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="包名.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
其中 file_paths 为res下新建 xml文件下文件
//<external-path/>
Environment.getExternalStorageDirectory(); /storage/emulated/0
//<external-cache-path/>
getExternalCacheDir(); /storage/emulated/0/Android/data/包名/cache
//<external-files-path/>
getExternalFilesDir(); /storage/emulated/0/Android/data/包名/files/Download
//<cache-path/>
getCacheDir();/data/user/0/包名/cache
//<files-path/>
getFilesDir(); /data/user/0/包名/files
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_storage_root"
path="." />
</paths>
这个例子中 使用 name=“external_storage_root” path="."。表示在/storage/emulated/0目录下所有文件在传递的时候会将/storage/emulated/0 替换为 external_storage_root
```java
file:///storage/emulated/0/QQBrowser/%E5%9B%BE%E7%89%87%E6%94%B6%E8%97%8F/d52a2834349b033b6ceadc1c1cce36d3d439bd91.jpg
变为
content://包名.provider/external_storage_root/QQBrowser/%E5%9B%BE%E7%89%87%E6%94%B6%E8%97%8F/d52a2834349b033b6ceadc1c1cce36d3d439bd91.jpg
包名.provider对应的为androidMainfest.xml中provider 的authorities属性值
2 使用时 代码中判断版本号
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri=FileProvider.getUriForFile(context,authorities属性值,File文件);
intent.addFlag(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else{
uri= Uri.fromFile(apkfile);
}
解决问题