android 6.0 更新apk失敗解決
阿新 • • 發佈:2019-02-01
一.清單檔案<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/rc_file_path" /> </provider> 二.res內建立XML資料夾新增一個rc_file_path.xml檔案如下; <paths> <external-path name="external_files" path="."/> </paths> 三.mainactivity內程式碼如下; private void installApk() { String fileStoreDir = this.getExternalCacheDir().getPath(); String fileName = "test" + "001" + ".apk"; File file = new File(fileStoreDir, fileName); Intent intent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= N) { Uri apkUri = FileProvider.getUriForFile(MainActivity.this, "包名.fileProvider", file); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); } startActivity(intent); }