最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年04月17日 09:53:07   作者:TheShy_  
這篇文章主要介紹了Android7.0行為變更之適配File Provider的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

兩個小解釋:

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

在Android 7.0的以上的系統(tǒng)中,嘗試傳遞file://URI可能會觸發(fā)FileUriExposedException

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

1.定義FileProvider

2.指定可用文件

3.檢索文件的Content URI

4.授予URI的臨時權(quán)限

5.將內(nèi)容URI提供給其他應(yīng)用程序

第一步:定義FileProvider:

//清單文件中
 <provider
    android:name="android.support.v4.content.FileProvider"http://固定
    android:authorities="${applicationId}.yourname"http://根據(jù)您控制的域?qū)傩栽O(shè)置為URI權(quán)限
    android:exported="false"http://FileProvider不需要公開
    android:grantUriPermissions="true">//允許您授予對文件的臨時訪問權(quán)限
    ...
</provider>

第二步:指定可用文件

//新建一個xml文件用于存放應(yīng)用需要共享的目錄文件
//以下paths元素告訴FileProvider您打算為images/私有文件區(qū)域的子目錄請求內(nèi)容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>

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

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

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

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

第三步:檢索文件的 Content URI

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

第四步:授予URI的臨時權(quán)限

授權(quán)方式有兩種:

第一種方式:

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

第二種方式:

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

第五步:將內(nèi)容URI提供給其他應(yīng)用程序

//通過以下方法啟動其他應(yīng)用并傳遞授權(quán)過的 Content URI 數(shù)據(jù)。當(dāng)然,也有其他方式提供服務(wù)。
startActivity() 

或者

startActivityResult()

或者

setResult()

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

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

場景:版本更新完成時打開新版本 apk 文件實(shí)現(xiàn)自動安裝

//在 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>
//授權(quán) 打開安裝管理器安裝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;
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

徐汇区| 苏尼特左旗| 麻城市| 西和县| 尚志市| 通辽市| 扎赉特旗| 长子县| 辽阳市| 太仆寺旗| 铁岭县| 时尚| 牡丹江市| 淅川县| 嘉定区| 沙雅县| 古丈县| 铁岭县| 什邡市| 黑水县| 庄河市| 镇远县| 天等县| 博野县| 荣昌县| 安国市| 边坝县| 诸暨市| 六枝特区| 大田县| 曲麻莱县| 六安市| 化德县| 潼关县| 伊金霍洛旗| 高阳县| 巴塘县| 霞浦县| 手游| 泸西县| 塘沽区|