標籤:系統 cat 成功 package get man acm fromfile VID
原文:Android項目實戰(四十):Andoird 7.0+ 安裝APK適配
首先看一下安裝apk檔案的代碼
/** * 通過隱式意圖調用系統安裝程式安裝APK */ public static void install(Context context) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile( new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "xxxx.apk")), "application/vnd.android.package-archive"); context.startActivity(intent); }
測試發現該段代碼在7.0一下的機型上可以成功開啟指定路徑下的指定apk檔案 , 但是在7.0+的機型上調用該代碼會報錯:
android.os.FileUriExposedException: file:///storage/emulated/0/Download/xxxx.apk exposed beyond app through Intent.getData()
原因在於:Android 7.0 版本開始 禁止向你的應用外公開 file:// URI。 如果一項包含檔案 file:// URI類型 的 Intent 離開你的應用,應用失敗,並出現 FileUriExposedException 異常。
解決方案:
一、在AndroidManifest.xml 檔案中添加 四大組件之一的 <provider>
<!-- 適配7.0 apk安裝 --> <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.xxx.xxxx.fileprovider" android:grantUriPermissions="true" android:exported="false"> <!--中繼資料--> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
注意這裡的 android :authorities 屬性的值 中的 com.xxx.xxxx 是你的包名,不可隨意填寫
二、res 目錄下 建一個xml 檔案,並建立xml檔案file_paths.xml
注意檔案名稱要和第一步中的 resource 屬性的值一致
內容為:
<?xml version="1.0" encoding="utf-8"?><paths> <external-path path="." name="download"/></paths>
三、根據機型的Android系統層級執行不同的安裝apk的代碼
注意,根據系統版本執行不同代碼,7.0以下調用7.0+的代碼會報錯,7.0+的調用7.0以下的會報錯。
if (file!=null){ // file 即 apk檔案 Intent intent = new Intent(Intent.ACTION_VIEW); // 由於沒有在Activity環境下啟動Activity,設定下面的標籤 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if(Build.VERSION.SDK_INT>=24) { //判讀版本是否在7.0以上 //參數1 上下文, 參數2 Provider主機地址 和設定檔中保持一致 參數3 共用的檔案 Uri apkUri = FileProvider.getUriForFile(context, "com.xxx.xxxxx.fileprovider", file); //添加這一句表示對目標應用臨時授權該Uri所代表的檔案 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); }else{ intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); } context.startActivity(intent); }
Android項目實戰(四十):Andoird 7.0+ 安裝APK適配