Android7.0系統(tǒng)使用Intent跳轉(zhuǎn)到APK安裝頁(yè)

轉(zhuǎn)載請(qǐng)注明出處:http://www.itdecent.cn/p/b566fa29a76e
本文出自Shawpoo的簡(jiǎn)書(shū)
我的博客:CSDN博客

前言

昨天在開(kāi)發(fā)的時(shí)候遇到這樣一個(gè)問(wèn)題,在A(yíng)PP中更新版本下載完最新的apk之后沒(méi)有跳轉(zhuǎn)到應(yīng)用安裝頁(yè)面。然后我換了個(gè)手機(jī)又進(jìn)行測(cè)試了一下是可以的,這就怪了。我的代碼是這樣寫(xiě)的:

    /**
     * @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);
    } 

打開(kāi)安裝頁(yè)面不就是這樣嗎?難道還有其他的方法?正在趕項(xiàng)目的我遇到這個(gè)問(wèn)題真是一臉懵逼,不知所措...


見(jiàn)鬼了?

查看了Studio的loacat才發(fā)現(xiàn)拋了一個(gè)異常:

FileUriExposedException

后來(lái)發(fā)現(xiàn)只有Android7.0的系統(tǒng)會(huì)報(bào)這個(gè)錯(cuò):

android.os.FileUriExposedException

為什么會(huì)這樣?難道是系統(tǒng)版本搞的鬼?沒(méi)錯(cuò),大胸弟,你的猜想一點(diǎn)沒(méi)錯(cuò),導(dǎo)致這個(gè)錯(cuò)誤就是由于A(yíng)ndroid7.0系統(tǒng)引起的。

查詢(xún)Android開(kāi)發(fā)官網(wǎng)可知:

了解:Android7.0 系統(tǒng)權(quán)限更改

為了提高私有文件的安全性,面向 Android 7.0 或更高版本的應(yīng)用私有目錄被限制訪(fǎng)問(wèn) (0700)。此設(shè)置可防止私有文件的元數(shù)據(jù)泄漏,如它們的大小或存在性。此權(quán)限更改有多重副作用:

上面三條更改總之就是,對(duì)用戶(hù)私有的文件訪(fǎng)問(wèn)更加嚴(yán)格,在訪(fǎng)問(wèn)的時(shí)候需要使用特定的類(lèi)和方法,而不像之前的系統(tǒng)那么容易,雖然這種限制不能完全得到控制,但是官方強(qiáng)烈反對(duì)放寬私有目錄的權(quán)限。

看到上面第二條,貌似和安裝APK所報(bào)的異常一樣,根據(jù)提示,看來(lái)需要使用FileProvider才能解決此異常的出現(xiàn)了。

進(jìn)入正題:開(kāi)始解決異常

1、定義FileProvider

在A(yíng)ndroidmanifest.xml文件中聲明:

<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.shawpoo.app.fileprovider" //自定義名字 為避免重復(fù)建議設(shè)為:包名.fileprovider
            android:exported="false"
            android:grantUriPermissions="true">
            ...
        </provider>
        ...
    </application>
</manifest>

2、指定可用的文件路徑

在項(xiàng)目的res目錄下,創(chuàng)建xml文件夾,并新建一個(gè)file_paths.xml文件。通過(guò)這個(gè)文件來(lái)指定文件路徑:

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

有多種指定路徑,在<paths>標(biāo)簽內(nèi)應(yīng)至少包含一種,或者多種。

  • a、表示應(yīng)用程序內(nèi)部存儲(chǔ)區(qū)中的文件/子目錄中的文件
<files-path name="name" path="image" />

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

  • b、表示應(yīng)用程序內(nèi)部存儲(chǔ)區(qū)緩存子目錄中的文件
<cache-path name="name" path="image" />

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

  • c、表示外部存儲(chǔ)區(qū)根目錄中的文件
<external-path name="name" path="image" />

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

  • d、表示應(yīng)用程序外部存儲(chǔ)區(qū)根目錄中的文件
<external-files-path name="name" path="image" />

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

  • e、表示應(yīng)用程序外部緩存區(qū)根目錄中的文件
<external-cache-path name="name" path="image" />

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

3、引用指定的路徑

在剛才Androidmanifest.xml中聲明的provider進(jìn)行關(guān)聯(lián):

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.shawpoo.app.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.shawpoo.app.fileprovider", newFile);

5、給Uri授予臨時(shí)權(quán)限

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

所以最終安裝apk的方法可以這么寫(xiě)了:

    /**
     * @param file
     * @return
     * @Description 安裝apk
     */
    protected void installApk(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0+以上版本
            Uri apkUri = FileProvider.getUriForFile(context, "com.shawpoo.app.fileprovider", file); //與manifest中定義的provider中的authorities="com.shawpoo.app.fileprovider"保持一致 
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }

總結(jié):好多東西還是得多查API吶~雖然英文是硬傷...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容