因 android 7.0的權限修改,原先的安裝apk的方法不能用,會報 android.os.FileUriExposedException 異常java
public void installApk(File file) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); }
使用fileprovider能夠解決這個問題。android
androidManifest.xml api
<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>
在res文件夾下建立 xml 文件夾,在xml下建立 file_paths.xmlapp
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <!--根目錄--> <external-path name="my_path" path="/"/> </paths>
<files-path name="name" path="image" /> = Context.getFileDir() : /data/data/com.xxx.app/files/image
ide
<cache-path name="name" path="image" /> = Context.getCacheDir() : /data/data/com.xxx.app/cache/image
ui
<external-path name="name" path="image" /> = Environment.getExternalStorageDirectory() : /storage/emulated/image 若是想爲根目錄 則把path = "/" 便可
this
<external-files-path name="name" path="image" /> = Context.getExternalFilesDir(String) / Context.getExternalFilesDir(null) : /storage/emulated/0/Android/data/com.xxx.app/files/image
spa
跳轉到安裝App的界面,兼容8.0code
/** * 安裝apk * @param fileUri apk文件的路徑 */ fun openAPKFile(fileUri: String?) { if (null != fileUri) { try { val intent = Intent(Intent.ACTION_VIEW) val apkFile = File(fileUri) //7.0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION val contentUri = FileProvider.getUriForFile(this, "$packageName.fileProvider", apkFile) intent.setDataAndType(contentUri, "application/vnd.android.package-archive") //8.0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val hasInstallPermission = packageManager.canRequestPackageInstalls() if (!hasInstallPermission) { // 提示語 startInstallPermissionSettingActivity() return } } } else { intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive") intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK } if (packageManager.queryIntentActivities(intent, 0).size > 0) { startActivity(intent) } } catch (e: Throwable) { e.printStackTrace() } } } /** * 跳轉到 設置-容許安裝未知來源 */ @RequiresApi(api = Build.VERSION_CODES.O) private fun startInstallPermissionSettingActivity() { //8.0新API val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) startActivity(intent) }