android 7.0安装apk失败

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

1.在AndroidManifest.xml中定义FileProvider:

xmlns:android="http://schemas.android.com/apk/res/android">
 
      android:name="android.support.v4.content.FileProvider"
    android:authorities="包名.fileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
            android:name="android.support.FILE_PROVIDER_PATHS"
        
        android:resource="@xml/file_paths" />
  

2.在res报下创建xml包并在xml包下创建file_paths.xml文件:

xml version="1.0" encoding="utf-8"?>

    path="Android/data/包名/" name="files_root" />
    path="." name="external_storage_root" />

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 ! ! !

你可能感兴趣的:(android)