android 7.0安装apk失败

来源:互联网 发布:红米note3数据连接不上 编辑:程序博客网 时间:2024/05/16 16:10

7.0中通过FileProvider 来对Content URI读取授权处理 可参考谷歌官网对FileProvider的说明点击打开链接。

1.在AndroidManifest.xml中定义FileProvider:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
 <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>

</application>
</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的时候检测并调用:

//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);}

 over ! ! !

原创粉丝点击