安卓

代码安装apk,解决Android7.0兼容问题

2018-06-14  本文已影响649人  程序狮

先上代码

public static boolean install(Context con, String filePath) {
        try {
            if(TextUtils.isEmpty(filePath))
                return false;
            File file = new File(filePath);
            if(file.exists()){
                return false;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//增加读写权限
            }
            intent.setDataAndType(getPathUri(con, filePath), "application/vnd.android.package-archive");
            con.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(con, "安装失败,请重新下载", Toast.LENGTH_LONG).show();
            return false;
        } catch (Error error) {
            error.printStackTrace();
            Toast.makeText(con, "安装失败,请重新下载", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }
public static Uri getPathUri(Context context, String filePath) {
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            String packageName = context.getPackageName();
            uri = FileProvider.getUriForFile(context, packageName + ".fileProvider", new File(filePath));
        } else {
            uri = Uri.fromFile(new File(filePath));
        }
        return uri;
    }

由于Android.N做了修改需要添加权限

1、在AndroidManifest中添加provider和权限

<application
  ...>
      <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>
</application>

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

2、在res下创建xml文件夹,接着在xml文件夹中传教file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <root-path name="root" path="" />
    <external-path name="external_storage_root" path="." />
    <external-path name="external_storage_download" path="Download" />
</paths>

说一下FileProvider

随着Android 7.0的到来引入“私有目录被限制访问”,“StrictMode API 政策”,为了进一步提高私有文件的安全性,Android不再由开发者放宽私有文件的访问权限,之前我们一直使用”file:///”绝对路径来传递文件地址的方式,在接收方访问时很容易触发SecurityException的异常。

因此,为了更好的适配Android 7.0,例如相机拍照这类涉及到文件地址传递的地方就用上了FileProvider,FileProvider也更好地进入了大家的视野。

其实FileProvider是ContentProvider的一个特殊子类,本质上还是基于ContentProvider的实现,FileProvider会把”file:///”的路径转换为特定的”content://”形式的content uri,接收方通过这个uri再使用ContentResolver去媒体库查询解析。

说一下file_paths.xml文件<paths>中可以定义的子节点
paths中可以定义的子节点.png

file://到content://的转换规则:

1.替换前缀:把file://替换成content://${android:authorities}。

2.匹配和替换

2.1.遍历的子节点,找到最大能匹配上文件路径前缀的那个子节点。

2.2.用path的值替换掉文件路径里所匹配的内容。

3.文件路径剩余的部分保持不变。


替换规则.png

注意

1、为了兼容8.0手机需要添加权限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

2、文件的路径必须包含在xml中,也就是2.1中必须能找到一个匹配的子节点

3、发现在华为mate9升级到Android8.0后,一直提示”安装解析包错误”,这个问题就是个例,我反馈给参考文章中的大牛,他反馈给华为内部,他们已经解决,升级到最新ROM就可以了。

参考文章

解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
华为android6.0,代码安装apk,不会弹出安装界面
安卓7.0遇到 android.os.FileUriExposedException: file:///storage/emulated.. exposed beyond app through Intent.getData()
对Provider详解

上一篇下一篇

猜你喜欢

热点阅读