Android 7.0系统代码调用安装apk时报错FileUriExposedException完美解决

admin 2021年6月7日21:21:50评论94 views字数 3057阅读10分11秒阅读模式

转载自我闺蜜Android 7.0系统代码调用安装apk时报错FileUriExposedException完美解决walker的微博



项目更新遇到问题

  Android项目开发中经常遇到下载更新的需求,以前调用系统安装器执行安装操作代码如下:

Intent intent = new Intent();intent.setAction(android.content.Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive"); context.startActivity(intent);

  如果Android系统为7.0及以上时则会报异常FileUriExposedException,这是由于安卓官方为了提高私有文件的安全性,面向 Android 7.0 或更高版本的应用私有目录被限制访问 (0700)。此设置可防止私有文件的元数据泄漏,如它们的大小或存在性。传递file:// URI 可能给接收器留下无法访问的路径。因此,尝试传递 file:// URI 会触发 FileUriExposedException。因此需要使用 FileProvider。

Android7.0系统使用FileProvider安装apk安装步骤:

1.manifest.xml文件配置:定义一个FileProvider

     <application        android:allowBackup="true"        android:label="@string/app_name"        android:icon="@mipmap/ic_launcher"        android:theme="@style/AppTheme">          <provider            android:name="android.support.v4.content.FileProvider"            android:authorities="packageName.fileprovider"            android:exported="false"            android:grantUriPermissions="true">             <meta-data                android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/file_paths"/>         </provider>      </application>      

2.添加可用权限的文件目录

  在项目res路径下新建名为xml的路径,在xml路径下新建名为file_paths.xml的文件,在file_paths.xml文件中增加如下内容指定分享的路径:

<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">     <external-path path="Android/data/packageName/" name="files_root" /></PreferenceScreen>

3.使用provider直接安装apk:

Intent intent = new Intent();intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", new File(path));intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setDataAndType(contentUri, context.getContentResolver().getType(contentUri)); //指定打开文件所调用的Activity,若不指定,则会弹出打开方式选择框,intent.setClassName("com.android.packageinstaller","com.android.packageinstaller.PackageInstallerActivity"); context.startActivity(intent); 

完美适配所有系统版本进行apk安装的方式

  如上代码虽然可以在Android7.0系统中正常安装apk,但是在低于Android7.0的系统中则不起作用,所以对apk安装调用方法进行封装,完美适配所有系统版本进行apk的安装调用。

/**      * 安装apk      *       * @param context Application对象      * @param path      *            apk路径      */     public static void InstallApk(Context context, String path) {          Intent intent = new Intent();        if (Build.VERSION.SDK_INT >= 24) {//Android 7.0以上             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);             Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", new File(path));             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);             intent.setDataAndType(contentUri, context.getContentResolver().getType(contentUri));            //指定打开文件所调用的Activity             intent.setClassName("com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");         } else {             intent.setAction(android.content.Intent.ACTION_VIEW);             intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");         }         context.startActivity(intent);


作者:walker-world

    

出处:http://www.cnblogs.com/walker-world/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。


本文始发于微信公众号(LemonSec):Android 7.0系统代码调用安装apk时报错FileUriExposedException完美解决

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年6月7日21:21:50
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Android 7.0系统代码调用安装apk时报错FileUriExposedException完美解决http://cn-sec.com/archives/301867.html

发表评论

匿名网友 填写信息