Android7.0 系统使用 Intent 跳转到 APK 安装页

我的博客:CSDN博客

前言

昨天在开发的时候遇到这样一个问题,在APP中更新版本下载完最新的apk之后没有跳转到应用安装页面。然后我换了个手机又进行测试了一下是可以的,这就怪了。我的代码是这样写的:

    /**
     * @param file
     * @return
     * @Description 安装apk
     */
    public void installApk(File file) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

打开安装页面不就是这样吗?难道还有其他的方法?正在赶项目的我遇到这个问题真是一脸懵逼,不知所措…

《Android7.0 系统使用 Intent 跳转到 APK 安装页》

见鬼了?

查看了Studio的loacat才发现抛了一个异常:

《Android7.0 系统使用 Intent 跳转到 APK 安装页》

FileUriExposedException

后来发现只有Android7.0的系统会报这个错:

android.os.FileUriExposedException

为什么会这样?难道是系统版本搞的鬼?没错,大胸弟,你的猜想一点没错,导致这个错误就是由于Android7.0系统引起的。

《Android7.0 系统使用 Intent 跳转到 APK 安装页》

查询Android开发官网可知:

了解:Android7.0 系统权限更改

为了提高私有文件的安全性,面向 Android 7.0 或更高版本的应用私有目录被限制访问 (0700)。此设置可防止私有文件的元数据泄漏,如它们的大小或存在性。此权限更改有多重副作用:

上面三条更改总之就是,对用户私有的文件访问更加严格,在访问的时候需要使用特定的类和方法,而不像之前的系统那么容易,虽然这种限制不能完全得到控制,但是官方强烈反对放宽私有目录的权限。

看到上面第二条,貌似和安装APK所报的异常一样,根据提示,看来需要使用FileProvider才能解决此异常的出现了。

进入正题:开始解决异常

1、定义FileProvider

在Androidmanifest.xml文件中声明:

<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mydomain.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            ...
        </provider>
        ...
    </application>
</manifest>

2、指定可用的文件路径

在项目的res目录下,创建xml文件夹,并新建一个file_paths.xml文件。通过这个文件来指定文件路径:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
           <files-path name="my_images" path="images/" />
        ...
</paths>

有多种指定路径,在<paths>标签内应至少包含一种,或者多种。

  • a、表示应用程序内部存储区中的文件/子目录中的文件
<files-path name="name" path="image" />

等同于Context.getFileDir() : /data/data/com.xxx.app/files/image

  • b、表示应用程序内部存储区缓存子目录中的文件
<cache-path name="name" path="image" />

等同于Context.getCacheDir() : /data/data/com.xxx.app/cache/image

  • c、表示外部存储区根目录中的文件
<external-path name="name" path="image" />

等同于Environment.getExternalStorageDirectory() : /storage/emulated/image

  • d、表示应用程序外部存储区根目录中的文件
<external-files-path name="name" path="image" />

等同于Context.getExternalFilesDir(String) / Context.getExternalFilesDir(null) : /storage/emulated/0/Android/data/com.xxx.app/files/image

  • e、表示应用程序外部缓存区根目录中的文件
<external-cache-path name="name" path="image" />

等同于Context.getExternalCacheDir() : /storage/emulated/0/Android/data/com.xxx.app/cache/image

3、引用指定的路径

在刚才Androidmanifest.xml中声明的provider进行关联:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.mydomain.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

4、生成文件的Uri

File imagePath = new File(Context.getFilesDir(), "images");
File newFile = new File(imagePath, "default_image.jpg");
Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);

5、给Uri授予临时权限

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

《Android7.0 系统使用 Intent 跳转到 APK 安装页》

所以最终安装apk的方法可以这么写了:

    /**
     * @param file
     * @return
     * @Description 安装apk
     */
    protected void installApk(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0+以上版本
            Uri apkUri = FileProvider.getUriForFile(context, "com.mydomain.fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }

总结:好多东西还是得多查API呐~虽然英文是硬伤…

    原文作者:Android
    原文地址: https://juejin.im/entry/58e4643db123db15eb79a902
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞