国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android7.0行為變更之適配File Provider的方法

2019-10-22 18:12:53
字體:
來源:轉載
供稿:網友

兩個小解釋:

FileProvider是ContentProvider特殊的子類,ContentProvider通過創建content:// Uri來替代file:/// Uri。

在Android 7.0的以上的系統中,嘗試傳遞file://URI可能會觸發FileUriExposedException

FileProvider的這個概述包括以下主題:

1.定義FileProvider

2.指定可用文件

3.檢索文件的Content URI

4.授予URI的臨時權限

5.將內容URI提供給其他應用程序

第一步:定義FileProvider:

//清單文件中 <provider    android:name="android.support.v4.content.FileProvider"//固定    android:authorities="${applicationId}.yourname"//根據您控制的域將屬性設置為URI權限    android:exported="false"//FileProvider不需要公開    android:grantUriPermissions="true">//允許您授予對文件的臨時訪問權限    ...</provider>

第二步:指定可用文件

//新建一個xml文件用于存放應用需要共享的目錄文件//以下paths元素告訴FileProvider您打算為images/私有文件區域的子目錄請求內容URI<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">  <files-path name="my_images" path="images/"/>  ...</paths>

該元素必須包含一個或多個以下子元素:

//代表內部存儲空間應用私有目錄下的 files/ 目錄,等同于 Context.getFilesDir() 所獲取的目錄路徑;<files-path name = “ name ” path = “ path ” />//代表內部存儲空間應用私有目錄下的 cache/ 目錄,等同于 Context.getCacheDir() 所獲取的目錄路徑;<cache-path name = “ name ” path = “ path ” />//代表外部存儲空間根目錄,等同于 Environment.getExternalStorageDirectory() 所獲取的目錄路徑;<external-path name = “ name ” path = “ path ” />//代表外部存儲空間應用私有目錄下的 files/ 目錄,等同于 Context.getExternalFilesDir(null) 所獲取的目錄路徑;<external-files-path name = “ name ” path = “ path ” />//代表外部存儲空間應用私有目錄下的 cache/ 目錄,等同于 Context.getExternalCacheDir();<external-cache-path name = “ name ” path = “ path ” />//代表外部媒體區域根目錄中的文件。等同于Context.getExternalMediaDirs()。<external-media-path name = “ name ” path = “ path ” />

這些子元素都使用兩個相同的屬性:

name="name"
一個URI路徑段。 用于給 path 屬性所指定的子目錄名稱取一個別名 為了提高安全性,此值將隱藏您要共享的子目錄的名稱。該值的子目錄名稱包含在該 path屬性中。
path="path"
你正在分享的子目錄。雖然該name屬性是一個URI路徑段,但該path值是實際的子目錄名稱。請注意,該值是指一個子目錄,而不是獨立文件名。您無法通過文件名共享單個文件,也無法使用通配符指定文件的子集。

第三步:檢索文件的 Content URI

//使用 FileProvider 類提供的公有靜態方法 getUriForFile 生成 Content URI//第一個參數:context上下文//第二個參數: Manifest 文件中注冊 FileProvider 時設置的 authorities 屬性值//第三個參數:要共享的文件,并且這個文件一定位于第二步我們在 path 文件中添加的子目錄里面Uri contentUri = FileProvider.getUriForFile(this,   BuildConfig.APPLICATION_ID + ".myprovider", myFile);

第四步:授予URI的臨時權限

授權方式有兩種:

第一種方式:

//調用方法://參數1:授權訪問 URI 對象的其他應用包名//參數2:授權訪問的 Uri 對象//參數3:授權類型FLAG_GRANT_READ_URI_PERMISSION 或者 FLAG_GRANT_WRITE_URI_PERMISSION    (或者二者同時授權。這種形式的授權方式,權限有效期截止至發生設備重啟或者手動調用 revokeUriPermission() 方法撤銷授權時)grantUriPermission(package, Uri, mode_flags)

第二種方式:

//配合intent使用//權限有效期截止至其它應用所處的堆棧銷毀,并且一旦授權給某一個組件后,該應用的其它組件擁有相同的訪問權限。Intent.setFlags() 或者 Intent.addFlags()Intent.setData(Uri uri);

第五步:將內容URI提供給其他應用程序

//通過以下方法啟動其他應用并傳遞授權過的 Content URI 數據。當然,也有其他方式提供服務。startActivity() 

或者

startActivityResult()

或者

setResult()

官方原文(需要自備梯子,想自己搭的教程點擊這里): Google Develpers - FileProvider

以下是一個我這邊的例子:

場景:版本更新完成時打開新版本 apk 文件實現自動安裝

//在 res/xml 目錄下新建一個filepath文件 并指定子目錄路徑信息<?xml version="1.0" encoding="utf-8"?><paths>  <external-path name="external_path" path="."/>  <cache-path name="cache_path" path="."/></paths>
//Manifest 文件中注冊 FileProvider 對象,并鏈接上面的 path 路徑文件<provider  android:name="android.support.v4.content.FileProvider"  android:authorities="com.xxx.FileProvider"  android:exported="false"  android:grantUriPermissions="true">  <meta-data    android:name="android.support.FILE_PROVIDER_PATHS"    android:resource="@xml/filepath"/></provider>
//授權 打開安裝管理器安裝apk包Intent intent = new Intent(Intent.ACTION_VIEW);intent.addCategory("android.intent.category.DEFAULT");intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);Uri uri = UriUtil.getUriForFile(BitZApplication.mContext.get(), new File((String) msg.obj));intent.setDataAndType(uri, "application/vnd.android.package-archive");startActivity(intent);//UriUtil工具類:public 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, "com.xxx.FileProvider", file);    } else {      uri = Uri.fromFile(file);    }    return uri;  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海阳市| 济源市| 登封市| 甘肃省| 屏山县| 邢台市| 新河县| 益阳市| 乌审旗| 河源市| 临安市| 扬中市| 江安县| 新邵县| 惠来县| 湘乡市| 礼泉县| 大埔县| 衢州市| 江华| 阿荣旗| 洛扎县| 永修县| 宁波市| 泊头市| 景洪市| 班玛县| 密云县| 密山市| 和硕县| 寻甸| 乡宁县| 东光县| 玉林市| 松阳县| 娄底市| 姜堰市| 西安市| 镇巴县| 临沭县| 枣庄市|