Android 7.0的适配问题,android.os.FileUriExposedException

来源:互联网 发布:mac如何装win10虚拟机 编辑:程序博客网 时间:2024/06/16 11:34

编写不易,如有转载,请声明出处: 梦回河口:http://blog.csdn.net/zxc514257857/article/details/70766998

报错问题:

  最近在集成AutoInstaller实现应用程序静默更新的时候出现了这样一个报错:

android.os.FileUriExposedException: file:///storage/emulated/0/Download/update-1.0.2.apk exposed beyond app through Intent.getData()

报错原因:

  Android 7.0以后提高了私有文件的安全性,其私有目录将会限制访问。若要在应用间共享文件,您需要发送一项 content:// URI,并授予 URI 临时访问权限。

解决方法:

AndroidManifest.xml中application节点下面添加如下代码:

<provider    android:name="android.support.v4.content.FileProvider"    android:authorities="top.wuhaojie.installerlibrary.fileProvider"    android:grantUriPermissions="true"    android:exported="false">    <meta-data        android:name="android.support.FILE_PROVIDER_PATHS"        android:resource="@xml/file_paths" /></provider>

  注:android:authorities=”你应用程序包名.fileProvider”

在app—>res下新建一个xml文件夹,文件夹中新建file_paths.xml文件,写入代码如下:

<paths>    <external-path path="Android/data/top.wuhaojie.installerlibrary/" name="files_root" />    <external-path path="." name="external_storage_root" /></paths>

  注: < external-path path=”Android/data/你应用程序包名/” name=”files_root” />

在报错代码的位置做如下修改(AutoInstaller源码第119行installUseAS()方法):

private void installUseAS(String filePath) {    File apkFile = new File(filePath);    Uri uri = Uri.fromFile(apkFile);    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(mContext, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);        intent.setDataAndType(contentUri, "application/vnd.android.package-archive");    }else{        intent.setDataAndType(uri, "application/vnd.android.package-archive");        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    }    mContext.startActivity(intent);    if (!isAccessibilitySettingsOn(mContext)) {        toAccessibilityService();        sendEmptyMessage(OPENSERVICE);    }}

参考资料:

  解决 Android N 7.0 上 报错:android.os.FileUriExposedException http://blog.csdn.net/yy1300326388/article/details/52787853

  在此项目中报出如下错误:
  Android中自己实现App一打开判断是否有更新,并通过依赖AutoInstaller实现自动更新
http://blog.csdn.net/zxc514257857/article/details/72667640

Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9850244


———-因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!———-

0 0
原创粉丝点击