7.0开发中Fileprovider在openFile中的使用,发现这篇博客写的已经很详细了,转载一下。

原文地址:http://blog.csdn.net/pkandroid/article/details/53716719

最近看到一个库,觉得有点意思,就下载源码编译了一下,结果发现打不开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()
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

图文: 

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

     "android.support.v4.content.FileProvider"            android:authorities="cn.bingoogolapple.update.demo.fileprovider"            android:grantUriPermissions="true"            android:exported="false"            >            "android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/file_paths" />                    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

注意:cn.bingoogolapple.update.demo 是包名。。。 

2、在res资源文件下新建目录xml,在xml目录下新建file_paths.xml文件,内容如下:

    <?xml version="1.0" encoding="utf-8"?>        "Android/data/cn.bingoogolapple.update.demo/"        name="files_root" />    "." name="external_storage_root" />                
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

注意: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);        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

如图:


如果包名写错了会空指针。。。

    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 referenceat 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)            
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

最后结果自然是打开了安装页面啦…这个是在项目里面直接修改的,如果在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

            

更多相关文章

  1. android 问题汇总系列之六
  2. Android(安卓)查看动态库依赖的库文件
  3. Android(安卓)SDK离线安装方法详解(加速安装) 转
  4. 关于上传的app的标识号和版本号
  5. 【黑马Android】(19)response下载文件/验证码/防盗链/URL编码/jsp
  6. Android(安卓)已申请权限仍然提示 open failed: EACCES (Permiss
  7. Android本地APP集成Mui框架
  8. Android上使用LibSVM
  9. Android(安卓)SDK目录结构和工具介绍

随机推荐

  1. Android(安卓)Asynchronous Http Client
  2. android 内部注册receiver
  3. Android快速显示4G
  4. Android(安卓)O(android 8.1) SYSTEM_UID应
  5. android opengl es 七彩星星效果
  6. Android(安卓)关闭所有Activity完全退出
  7. 【AndroidStudio】Aapt2Exception
  8. Android(安卓)OnTouchListener 触摸事件
  9. android porting experience
  10. android常用网站