当前位置: 移动技术网 > 移动技术>移动开发>Android > Android项目开发:Andoird 7.0及安装APK适配

Android项目开发:Andoird 7.0及安装APK适配

2018年03月23日  | 移动技术网移动技术  | 我要评论
首先看一下安装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 文件中添加 四大组件之一的

<>< p=""> <>

android:name="android.support.v4.content.FileProvider"

android:authorities="com.xxx.xxxx.fileprovider"

android:grantUriPermissions="true"

android:exported="false">

<>< p=""> <>

<>< p=""><>< p="">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths" />

注意这里的 android :authorities 属性的值 中的 com.xxx.xxxx 是你的包名,不可随意填写

二、res 目录下 建一个xml 文件,并新建xml文件file_paths.xml

注意文件名要和第一步中的 resource 属性的值一致

内容为:

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

    "." name="download"/>

三、根据机型的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以上

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);

}

<><>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网