1. 程式人生 > >Android 7.0 更新APK在安裝時的報錯 android.os.FileUriExposedException(已修復)

Android 7.0 更新APK在安裝時的報錯 android.os.FileUriExposedException(已修復)

1.在網上我們能看到類似很多問題多數都存在很多相似問題,很多細節沒有講清楚

如果出現了FIleUriExposedException異常大家可以去https://developer.android.google.cn/about/versions/nougat/android-7.0-changes.html (android行為變更)具體瞭解一下


這個問題是由於 Android 7.0 許可權更改導致,確切的講是 Android 對許可權的進一步管理,從 Android 6.0 的動態許可權申請到這個問題可以看出 Google 也是越來越重視 Android 環境的安全問題了。

解決方案

安裝官方給的步驟:

1在androidmanifest.xml 新增

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="app包名.fileProvider"        右鍵點選Copy Referrence即可複製包名
android:grantUriPermissions="true"
android:exported="false">
    <meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/> </provider>

其中 authorities:app的包名.fileProvider

grandtUriPermissions  必須是為true 表示授權URI臨時訪問

exported :必須是false

resource:中的@xml/file_paths    這個需要我們先新建一個xml文件 (右鍵res->New Android Resource Directory 然後在value 下拉選擇xml檔案)


2 開啟file_paths.xml檔案新增:

<paths>
<external-path path="Android/data/app包名/" name="files_root"/> <external-path path="." name="external_storage_root"/> </paths>

<external-cache-path> 表示應用程式內部儲存目錄下的 cache/ 目錄,完整路徑為 Android/data/com.xxx.xxx/cache/
path 屬性用於指定子目錄。
name 屬性告訴 FileProvider 為 Android/data/com.xxx.xxx/cache/app/ 建立一個名為 apk 的路徑欄位。
想要通過 FileProvider 為檔案生成 content URI 只能在此處指定目錄,以上示例就表示我將要共享 Android/data/com.xxx.xxx/cache/app/ 這個目錄,除此之外還可以共享其它目錄,對應的路徑如下:
標籤 路徑
<files-path name="name" path="path" /> Context.getFilesDir()           

/data/user/0/com.example.wangsl.hellomacpro/files

<cache-path name="name" path="path" /> getCacheDir()

 /data/user/0/com.example.wangsl.hellomacpro/cache

<external-files-path name="name" path="path" /> Context.getExternalFilesDir()

/storage/emulated/0/Android/data/com.example.wangsl.hellomacpro/files

<external-cache-path name="name" path="path" /> Context.getExternalCacheDir()
/storage/emulated/0/Android/data/com.example.wangsl.hellomacpro/cach

前面兩個是android自身包含的儲存容量,而後三個是存在sd卡時的路徑。 3 然後根據官方文件我們需要把file:// 換成url形式
  Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
// 設定intent的file與MimeType
//                intent.setDataAndType(Uri.fromFile(new File(mLocalFilepath)),  //這個時出錯的程式碼 我們不能直接file://形式
//                        "application/vnd.android.package-archive");
intent.setDataAndType(getUriForFile(mContext, new File(mLocalFilepath)),
"application/vnd.android.package-archive");
//其中getUriForFile函式
private static Uri getUriForFile(Context context, File file) {
    if (context == null || file == null) {
        throw new NullPointerException();
}
    Uri uri;
    if (Build.VERSION.SDK_INT >= 24) {
        uri = FileProvider.getUriForFile(context.getApplicationContext(), "com.example.wangsl.appupdatetext.fileProvider", file);
} else {
        uri = Uri.fromFile(file);
}
    return uri;
}


總結:先了解原理,不然下一次出錯還是得上網找半天。 除了上面這個問題,在 Android 7.0 之前開發的分享圖文、瀏覽編輯本地圖片、共享互傳檔案等功能如果沒有使用 FileProvider 來生成 URI 的話,在 Android 7.0 上就必須做這種適配了,所以平時建議大家多關注 Android 新的 API ,儘早替換已被官方廢棄的 API ,實際上 FileProvider 在 API Level 22 已經添加了。

相關推薦

Android 7.0 更新APK安裝時的報 android.os.FileUriExposedException修復

1.在網上我們能看到類似很多問題多數都存在很多相似問題,很多細節沒有講清楚 如果出現了FIleUriExposedException異常大家可以去https://developer.android.google.cn/about/versions/nougat/androi

Android 7.0 下載APK後自動安裝

mime 顯示 sse cast works 入口 新的 限制 itl 隨著Android版本越來越高,Android對隱私的保護力度也越來越大。這些隱私權限的更改在為用戶帶來更加安全的操作系統的同時也為開發者帶來了一些新的任務。如何讓你的APP能夠適應這些

Android 7.0: install .apk

最後是試出來了,中間有幾個問題,就是安裝是從內部 cache dir 會有問題,安裝程式會顯示: there was a problem parsing the package 改成 external cache dir 就OK。所以 xml 檔一定要使用下面這一組: <external-path

Android 7.0呼叫相機拍照 報 FileUriExposedException

摘自郭霖 《第一行程式碼》 從Android 7.0系統開始,直接使用本地真實路徑的Uri,被認為是不安全的,會丟擲一個FileUriExposedException異常,,而FileProvider則是一種特殊的內容提供器,它使用了和內容提供器類似的機制來對

android 6.0 更新apk失敗解決

一.清單檔案<application>內新增如下; <provider android:name="android.support.v4.content.

Android 7.0適配 afinal報

最近專案碰到7.0版本執行直接閃退 錯誤日誌如下 com.android.internal.policy.DecorContext cast to activity 通過列印日誌和檢視原始碼得知 net.tsz.afinal.AFinalUIHelp

Android 7.0系統原始碼中如何編譯android studio工程

eclipse工程放在系統原始碼裡編譯,直接把整個包丟到系統裡,然後加一個Android.mk,就完事了,可是eclipse被谷歌拋棄很久了,現在琢磨者如何將android studio工程放到系統原始碼裡編譯,在網上找了一大圈 都沒有找到,只好自己幹了了。 android

PopWindow Android 7.0位置顯示不準確以及Android 8.0全面屏顯示導航鍵留白解決辦法

popWindow 在Android7.0上的顯示位置不管怎麼設定都在螢幕的頂部,這是7.0的bug,已在7.1修復,但是7.0還是需要我們自己解決的,以及在小米mix2全面屏導航鍵留白,顯示不全。如圖: 方法如下: @Override public void sho

Android 6.0 HTTPS SSL 無法訪問,提示Handshake failed握手失敗,解決方案

前言 之前開發的一個專案使用的是http請求,但是安全公司給出了一個安全報告,建議使用https協議來訪問網路資源,使用私簽證書來實現了https。Android 6.0以下的版本均可以使用,Android 6.0及以上的機型請求成功,並且在logcat中有H

Android 7.0 app升級內部安裝apk—FileProvider使用

隨著Android 版本升級 Google在保護使用者隱私和安全方面增加了力度 像Android 6.0 細分了普通許可權和高危許可權,涉及到高危許可權則需要及時申請使用者必須感知。Android7.0 增加了 "StrictMode API" 政策即私有目錄限制訪問。本篇講解一下如何在Androi

Android 6.0 / 7.0 / 8.0APK安裝

Android 1.x~6.x APK安裝 關於 android 7.0 之前的 apk安裝,需要注意設定 Intent.FLAG_ACTIVITY_NEW_TASK 就好 Intent intent = new Intent(Intent.ACTION_VIEW);

android 7.0安裝apk失敗

7.0中通過FileProvider 來對Content URI讀取授權處理 可參考谷歌官網對FileProvider的說明點選開啟連結。 1.在AndroidManifest.xml中定義FileProvider: <manifest xmlns:android="http

android 下載apk安裝apk(適配android 7.0)

    為適配7.0以後系統,首先需要在AndroidManifest.xml檔案中application節點下定義provider,如下:   <provider          android:nam

android app 更新下載安裝 適配android 7.0

public class UpdataService extends Service { public UpdataService() { } /** * 安卓系統下載類 **/ private DownloadManager manager; /

android 7.0之後安裝apk

android 7.0行為變更中系統許可權的更改裡面提到: 傳遞軟體包網域外的 file:// URI 可能給接收器留下無法訪問的路徑。因此,嘗試傳遞 file:// URI 會觸發 FileUriE

appium在android 7.0真機上運行報command failed shell:............ps:'uiautomator"的解決方式

.proto androi sse ces node return std tomato 並且 appium版本:1_4_16 在CSDN中找到相關解決的方案,根據此解決方案順利的解決了讓人惆悵的問題,再次記錄。 1、找到appium安裝目錄下的adb.js文件,目錄為:A

Android 7.0以上系統獲取以content開頭的檔案拿不到正確路徑,報column-data-does-not-exist

我們專案的使用場景,手機qq開啟檔案,選擇其他開啟方式,選擇我們自己的應用開啟,通過intent.getData()獲取檔案地址,後來發現在Android7.0之後的版本,獲取到的地址不正確,說檔案不存在。日誌報錯column-data-does-not-exist, 經過一天的折騰,終於在網上

android 7.0 root下靜默安裝

公司產品基於7.0開發,需要實現覆蓋升級。之前的產品覆蓋升級基於5.1系統,在7.0上不起作用。網上參考了許多部落格。最終稀裡糊塗的成功了,簡單記錄。 核心程式碼只有一句 String command

為什麼應用程式在Android 7.0之後安裝和執行都變得更快?

需要了解幾個概念 Dalvik 虛擬機器 負責解釋dex檔案為機器碼,每次執行程式碼,都需要Dalvik將dex程式碼翻譯為微處理器指令,然後交給系統處理,這樣效率不高。 JIT(Just-In-Time) 為了解決上面的問題,Google在2

npm安裝時報---npm ERR! Windows_NT 10.0.16299

npm ERR!Windows_NT10.0.16299 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe""C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cl