FileUriExposedException exposed beyond app through Intent.getData()

沒有加額外的筆記 ,請看原文:xx.apk exposed beyond app through Intent.getData()

輔助文章:FileProvider Android7.0 (文件共享,使用系統播放器打開視頻等等)


絕大多數國產Android App都會內置一個更新功能,也就是把新版本的APK放在服務器上,通過接口獲取更新信息並下載,然後進行安裝。雖然這種行爲被Google嚴厲禁止,但身處這種環境下還是得妥協的。

絕大多數的經驗人士都知道以往我們在App內部安裝新版本APK的時候,只需要使用非常簡單的代碼就能實現:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
finish();
startActivity(intent);

其中,file參數是一個通過APK文件的地址獲取的File對象,比如你的APK下載地址是/sdcard/myapp.apk,則傳入new File(“sdcard/myapp.apk”)。簡單粗暴,效果顯著。到了Android 7.0之後,繼續沿用這部分代碼的時候,就會發現問題了

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

FileUriExposedException字面意思是,文件Uri暴露的異常

當你的應用把file:// Uri暴露給其他App的時候就會出現這種異常,因爲接收方Ap可能並不具備訪問該共享資源的權限。所以應該用content:// Url來拓展臨時權限,這樣接收方就能訪問到資源了。顯然,這是Google爲了收緊Android的自由度,提升安全度所做的事情

在應用間共享文件
對於面向 Android 7.0 的應用,Android 框架執行的 StrictMode API 政策禁止在您的應用外部公開 file:// URI。如果一項包含文件 URI 的 intent 離開您的應用,則應用出現故障,並出現 FileUriExposedException 異常。

要在應用間共享文件,您應發送一項 content:// URI,並授予 URI 臨時訪問權限。進行此授權的最簡單方式是使用 FileProvider 類

解決方法:

AndroidManifest中增加
       <provider
           android:name="android.support.v4.content.FileProvider"
           android:authorities="當前包名.fileprovider"
           android:exported="false"
           android:grantUriPermissions="true">
           <meta-data
               android:name="android.support.FILE_PROVIDER_PATHS"
               android:resource="@xml/filepaths"
               />
       </provider>

AndroidX第一行應變爲

androidx.core.content.FileProvider

res下新建xml/filepaths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external" path=“"/>
</paths>

使用FileProvider兼容安裝apk
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    File apkFile = new File(apkSavePath);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        Uri uri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".fileprovider", apkFile);
                        intent.setDataAndType(uri, "application/vnd.android.package-archive");
                    } else {
                        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                    }
                    startActivity(intent);

其中apkSavePath,我的程序放到了sd卡下

       //獲取SD卡的根路徑
        String sdcardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
        final String apkSavePath = sdcardRoot+"/1.apk";

對這些代碼做些解釋,如果系統低於Android 7.0,則還是使用老一套,否則就要走FileProvider路線
給intent設置了FLAG_GRANT_READ_URI_PERMISSION後,就代表在啓動第三方Activity(實際上就是你要把自己的文件共享給的第三方App)的時候,授予對方臨時讀取URI所映射的目錄的權限
"Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + “.fileprovider”, apk_file);"則通過FileProvider將我們的文件(這裏就是新版本的APK文件)的路徑轉化爲了content://Uri,這樣就實現了可保證安全的臨時文件共享功能,如此一來系統負責處理該intent的應用就能順利安裝APK了


————————————————
版權聲明:本文爲CSDN博主「Errol_King」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/u010356768/article/details/89212742

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章