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

先上代码

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详解

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,098评论 25 709
  • 转载自于连林 我们使用手机的时候经常会看到应用程序提示升级,大部分应用内部都需要实现升级提醒和应用程序文件(APK...
    sirai阅读 4,918评论 0 24
  • 文 / 金小札 一个严肃、不逗比的85后 人民日报整版发问:如何给宝宝更适合的爱?“中国八大权威媒体齐声回应“陪着...
    金小扎阅读 8,916评论 0 1
  • 题记:很多年后才发现,很多东西己经离我们越来越远。 但我们仍然很纯真,因为好奇心还在我们的心里。 作为一个南方人,...
    jihua_usc阅读 1,781评论 0 0
  • 被邻里冲破门进到屋里,他一个人安然躺在床上,无声无息。没人知道,他倒在了黎明前,还是陨落在长夜?唯一的线索是,相熟...
    路小山阅读 2,854评论 2 3