Android开发 android7.0安装apk文件的姿势

最近看到一个库,觉得有点意思,就下载源码编译了一下,结果发现打不开apk包,报错为:

    //这个库的地址是:https://github.com/bingoogolapple/BGAUpdate-Android
    //设置了超链接也不变色,还是直接写出来得了

      Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/cn.bingoogolapple.update.demo/files/apk/BGAUpdateDemo_v1.0.0.apk exposed beyond app through Intent.getData()

图文:
这里写图片描述

看了下,估计是没有兼容android7.0的原因,这个作者的gradle设置的比较另类,我是第一次见,就没改,提交了issues,然后呢作者回复我让我改,估计他手头没有7.0的机器吧,我的也是前两天才升级了…
在这里把解决步骤记录一下..
1、在AndroidManifest.xml配置清单的Application中添加

     <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="cn.bingoogolapple.update.demo.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false"
            >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

注意:cn.bingoogolapple.update.demo 是包名。。。
这里写图片描述
2、在res资源文件下新建目录xml,在xml目录下新建file_paths.xml文件,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
    <external-path path="Android/data/cn.bingoogolapple.update.demo/"        name="files_root" />
    <external-path path="." name="external_storage_root" />
    </paths>

注意:cn.bingoogolapple.update.demo 是包名..
这里写图片描述
3、然后就是对安装apk方法的更改:

        /**
     * 安装 apk 文件
     *
     * @param apkFile
     */
    public static void installApk(File apkFile) {
       /* Intent installApkIntent = new Intent();
        installApkIntent.setAction(Intent.ACTION_VIEW);
        installApkIntent.addCategory(Intent.CATEGORY_DEFAULT);
        installApkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        installApkIntent.setDataAndType(Uri.fromFile(apkFile), MIME_TYPE_APK);

        if (sApp.getPackageManager().queryIntentActivities(installApkIntent, 0).size() > 0) {
            sApp.startActivity(installApkIntent);
        }*/
        //Toast.makeText(sApp,apkFile.getPath(),Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(sApp, "cn.bingoogolapple.update.demo.fileprovider", apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if (sApp.getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
            sApp.startActivity(intent);
        }
    }

如图:

这里写图片描述
如果包名写错了会空指针。。。

    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)

最后结果自然是打开了安装页面啦…这个是在项目里面直接修改的,如果在library里面修改,步骤是一样的
这里写图片描述
//系统截图/
2016年12月17日23:35:21
这里写图片描述
如果你想下载这个demo来看看,点击下载,不过最好还是看作者的,相信他已经兼容了吧,哈
下载地址:
java
作者的github:https://github.com/bingoogolapple/BGAUpdate-Android
当前版本:
http://download.csdn.net/detail/pkandroid/9714442

————————–2017年3月9日16:44:11———————————–
注意:如果AndroidManifest.xml中配置的provider的authorities名称不能重复,如果在别的APP中使用了这个provide的authorities属性(比如APP对于Android7.0之后的拍照的适配),那么这个APP可能会安装不上,提示卸载含有相同provider authorities属性的APP

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值