7.0中通过FileProvider 来对Content URI读取授权处理 可参考谷歌官网对FileProvider的说明点击打开链接。
1.在AndroidManifest.xml中定义FileProvider:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
</application><provider android:name="android.support.v4.content.FileProvider"android:authorities="包名.fileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS"<!--定义自己的存储路径文件--> android:resource="@xml/file_paths" /> </provider>
</manifest>
2.在res报下创建xml包并在xml包下创建file_paths.xml文件:
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path path="Android/data/包名/" name="files_root" /> <external-path path="." name="external_storage_root" /> </paths>
3.在安装apk的时候检测并调用:
over ! ! !//mSavePath :apk的下载到本地的存储路径
//fileName:apk的名字
File file = new File(mSavePath, fileName);Uri downloadFileUri = Uri.fromFile(file); Intent install = new Intent(Intent.ACTION_VIEW); if (downloadFileUri != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(context, "包名.fileProvider", file); install.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } context.getApplicationContext().startActivity(install); }