背景
这里主要是根据亚特兰蒂斯的博文https://blog.csdn.net/myatlantis/article/details/52874227#t1进行操作的,但即便是一步一步走,还是遇到了一些坑,特记录如下
坑和解决方法
1、安卓高版本文件uri权限问题
安卓高版本里访问文件uri必须用FileProvider,用法请参见这篇文章:安卓开发学习之利用FileProvider获取文件URI
2、通过intent安装apk遇到包解析错误
要给intent添加读取uri的权限,相关完整代码如下
public static void installApk(Context context, String apkPath) { File file = new File(apkPath); if (file.exists()) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 添加读取uri权限 intent.setDataAndType(FileProvider.getUriForFile(context, "com.example.songzeceng.myndkdemo.myFileProvider", file), "application/vnd.android.package-archive"); context.startActivity(intent); } }