Android7.0使用FileProvider安装apk
来源:互联网 发布:淘宝链接转微信二维码 编辑:程序博客网 时间:2024/06/01 08:19
Android7.0安装文件的话默认会抛出FileUriExposedException 解决方法:
1.在AndroidManifest.xml中activity同节点下定义一个fileProvider
<manifest> ... <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/file_paths" /> </provider> ... </application></manifest>
2.在res目录下,增加xml文件夹,并新建一个名为 file_paths.xml 的文件
<paths><external-path path="包名/" name="files_root" /></paths>
3.安装时调用此方法
//安装文件(适配Android7.0) public static void installApk(Context context,String filePath) { Intent intent = new Intent(Intent.ACTION_VIEW); //判断是否是AndroidN以及更高的版本 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", new File(filePath)); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } context.startActivity(intent); }
遇到的坑
Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
解决方法:
检查AndroidManifest.xml中 android:authorities="包名.fileProvider" 是否与安装的方法中 FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", new File(filePath)); 检查是否相同,注意大小写
阅读全文
0 0
- Android7.0使用FileProvider安装apk
- android7.0 FileProvider配置安装apk文件的姿势
- Android7.0 使用FileProvider 共享文件
- Android7.0之FileProvider
- Android7.0的FileProvider
- Android7.0 apk安装
- 快速使用FileProvider解决Android7.0文件权限问题
- Android7.0使用DownloadManager和FileProvider更新App
- 快速使用FileProvider解决Android7.0文件权限问题
- Android7.0的FileProvider适配
- Android7.0 PackageManagerService APK安装
- Android7.0系统使用Intent跳转到APK安装页
- Android7.0系统使用Intent跳转到APK安装页
- androidN通过intent安装apk需要使用FileProvider
- Android 7.0 app升级内部安装apk—FileProvider使用
- Android7.0 应用间共享文件 FileProvider
- Android7.0文件操作之FileProvider
- 兼容Android7.0自动安装 FileProvider 解决下载完成后无法自动安装
- listview嵌套listview
- xshell4 选中复制,右键粘贴
- react-native setNativeProps进阶
- 初中经典几何题,95%的同学都不会做,高手请进来 2016-03-21 16:25 几何是初中数学最主要的内容,对大多数孩子来说也是比较难的内容。而我们想要战胜这一比较难的题型,我们就需要多多练题
- java实现简单员工类
- Android7.0使用FileProvider安装apk
- XML学习总结——XML入门
- video标签的播发次数
- 关于web开发中未知div高宽度的垂直居中
- 群晖Docker通过Dockerfile构建镜像 部署Tomcat
- 00--三修python
- andriod两次返回退出程序实现
- php的几种运行模式CLI、CGI、FastCGI、mod_php
- Exynos4412 Uboot 移植(二)—— Uboot 启动流程分析