android 使用FileProvider 兼容apk 在7.0版本无法安装

本文介绍如何解决Android 7.0及以上版本系统中出现的FileUriExposedException错误,通过使用FileProvider实现跨应用安装APK的兼容方案,并提供具体实现步骤和代码示例。

源头

都是因为一个bug

android.os.FileUriExposedException: 
file:///storage/emulated/0/mwh/app.apk exposed beyond app through Intent.getData()

因为这个bug 显示 原来的 apk安装方法无法在高于7.0 的手机系统上安装apk了。

所以就得需要使用FileProvider 来做兼容

安装apk 兼容7.0以上系统

配置mainfest 信息

在mainfest 的 application 中 添加如下代码



        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
  • android:authorities 这里使用的是包名。这个其实可以随便写,写成123 都可以。
  • android:resource 这里配置一个 xml文件。名字也可以随便写 这里就叫 file_paths好了

配置xml 信息

这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="." path="."/>
</paths>
这里配置的很简单 ,
external-path 是放到sd卡的目录下 Environment.getExternalStorageDirectory()

path="." 代表共享 sd卡下的所有目录。手机会遍历sd卡下的所有目录,来匹配你要安装 的apk 的目录。。
name="." 代表 你apk存放目录下所有apk的名字都会遍历一遍,然后跟你要安装的apk进行匹配

安装apk代码


    /**
     * 安装apk
     *
     * @param fileSavePath
     */
    private void installApk(String fileSavePath) {
        File file = new File(fileSavePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri data;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0
            // "sven.com.fileprovider.fileprovider"即是在清单文件中配置的authorities
            // 通过FileProvider创建一个content类型的Uri
            data = FileProvider.getUriForFile(this, "sven.com.fileprovider.fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 给目标应用一个临时授权
        } else {
            data = Uri.fromFile(file);
        }
        intent.setDataAndType(data, "application/vnd.android.package-archive");
        startActivity(intent);
    }

其他

为了演示效果。我把事先准备好的apk。放到了assets 目录下
点击安装 按钮,会把apk 赋值到 指定目录下,然后在 进行安装操作。


    /**
     * 如果sdcard没有文件就复制过去
     */
    private String copyFile() {
        AssetManager assetManager = this.getAssets();
        String newFilePath = Environment.getExternalStorageDirectory() +"/mwh/app-release.apk";
        String Path = Environment.getExternalStorageDirectory() + "/mwh";
        try {
            File file1 = new File(Path);

            if (!file1.exists()) {
                file1.mkdir();
            }

            File file = new File(newFilePath);
            if (!file.exists()) {//文件不存在才复制
                InputStream in = assetManager.open("app-release.apk");
                OutputStream out = new FileOutputStream(newFilePath);
                byte[] buffer = new byte[1024];
                int read;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
                in.close();
                out.flush();
                out.close();
            }

        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
        return newFilePath;
    }
  • 注意
   String newFilePath = Environment.getExternalStorageDirectory() +"/mwh/app-release.apk";

这里的目录是 sd 卡目录下的mwh 文件夹。我们直接使用

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="." path="."/>
</paths>
就可以了。

最后

到这里就结束了。 不懂的可以把代码下载下来 试一下
https://github.com/wanghao200906/FileProviderDemo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值