android 使用fileprovide 安装apk文件
因 android 7.0的权限修改,原先的安装apk的方法不能用,会报 android.os.FileUriExposedException 异常
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可以解决这个问题。
androidManifest.xml
<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.xml
<?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
<cache-path name="name" path="image" /> = Context.getCacheDir() : /data/data/com.xxx.app/cache/image
<external-path name="name" path="image" /> = Environment.getExternalStorageDirectory() : /storage/emulated/image 如果想为根目录 则把path = "/" 即可
<external-files-path name="name" path="image" /> = Context.getExternalFilesDir(String) / Context.getExternalFilesDir(null) : /storage/emulated/0/Android/data/com.xxx.app/files/image
跳转到安装App的界面,兼容8.0
/**
* 安装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)
}
浙公网安备 33010602011771号