文件地址
从Android 7.0开始,不再允许在app中把file:// Uri暴露给其他app,否则应用会抛出FileUriExposedException。原因在于,Google认为使用file:// Uri存在一定的风险。比如,文件是私有的,其他app无法访问该文件,或者其他app没有申请READ_EXTERNAL_STORAGE运行时权限。解决方案是,使用FileProvider生成content:// Uri来替代file:// Uri。
解决步骤
1.声明FileProvider 首先在清单文件中申明FileProvider。
<manifest>
...
<application>
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
...
</application>
</manifest>
- android:name是固定写法。
- android:authorities可自定义,是用来标识该provider的唯一标识,建议结合包名来保证authority的唯一性。
- android:exported必须设置成 false,否则运行时会报错java.lang.SecurityException: Provider must not be exported 。
- android:grantUriPermissions用来控制共享文件的访问权限。
2.添加file_paths.xml文件 文件格式如下
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--<external-path path="Android/data/zhwl.com.simplsale/" name="files_root" />-->
<!--<external-path path="." name="external_storage_root" />-->
<external-files-path name="update_download" path="download" />
<external-path name="external_files" path="."/>
<cache-path
name="picture_files"
path="."/>
</paths>
其中根元素<paths>是固定的,内部元素可以是以下节点:
- <files-path name="name" path="path" /> 对应getFilesDir()。
- <cache-path name="name" path="path" /> 对应getCacheDir()。
- <external-path name="name" path="path" /> 对应Environment.getExternalStorageDirectory()。
- <external-files-path name="name" path="path" /> 对应getExternalFilesDir()。
- <external-cache-path name="name" path="path" /> 对应getExternalCacheDir()。
3.在Java代码中使用FileProvider
/**
* 安装apk
*
* @param
*/
private void installApk(File apkFile) {
if (!apkFile.exists()) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
mContext.startActivity(intent);
}