? 近期需要用到讀取Android設(shè)備外存中的JSON和Word,在前期開發(fā)時(shí)用于調(diào)試的是Android 6.0的米4,當(dāng)拿到實(shí)際應(yīng)用環(huán)境(Android 7.0)時(shí),Android 6.0之后的版本增加了運(yùn)行時(shí)權(quán)限,應(yīng)用程序在執(zhí)行每個(gè)需要系統(tǒng)權(quán)限的功能時(shí),需要添加權(quán)限請(qǐng)求代碼(默認(rèn)權(quán)限禁止),否則應(yīng)用程序無法響應(yīng);查閱官方文檔后發(fā)現(xiàn)可以使用FileProvider解決該問題
FileProvider概述
官方描述:FileProvider是ContentProvider的一個(gè)特殊的子類,通過創(chuàng)建content:// Uri來替代file:///Uri分享文件給另一個(gè)App,來促進(jìn)安全共享。
原文: FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri.
主要步驟:
- 定義一個(gè)FileProvider
- 指定可用文件
- 檢索文件的內(nèi)容URI
- 授予URI的臨時(shí)權(quán)限
- 將內(nèi)容URI提供給其他應(yīng)用程序
官方文檔鏈接:(https://developer.android.com/reference/android/support/v4/content/FileProvider.html#ServeUri)
定義一個(gè)FileProvider
- 首先需要在AndroidManifest.xml里申請(qǐng)關(guān)于文件的權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
或者
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
-
同樣,在AndroidManifest.xml中添加以下代碼,允許授權(quán)臨時(shí)訪問文件
<manifest> ... <application> ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.mydomain.fileprovider" android:exported="false" android:grantUriPermissions="true"> ... </provider> ... </application> </manifest>
指定可用文件
- 在
res下新建一個(gè)xml文件夾,并新建一個(gè)filepath.xml文件,使用paths指定具體文件路徑,
在 paths 中 name 屬性是用于之后的調(diào)用,path 屬性對(duì)應(yīng)我們需要的文件目錄在對(duì)應(yīng)父節(jié)點(diǎn)的路徑
示例如下
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-cache-path name="external_storage" path="docs/"/>
</paths>
- 創(chuàng)建后需要在fileprovider中加入該xml文件
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.yyyyz.systonpad.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
? 在 provider 中加入一個(gè) meta-data 元素 , android:name 屬性設(shè)為以上默認(rèn)值即可, android:resource 設(shè)為剛才所添加的xml文件文件名。
為文件生成Content Uri
在完成以上兩步后即可在程序中引用你所需要的文件,示例如下
File docPath = new File(mContext.getExternalCacheDir(), "docs");
File file = new File(docPath, cadre.getXm() + ".doc");
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Uri uri = getUriForFile(mContext,
BuildConfig.APPLICATION_ID+ ".fileprovider",
file);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// 設(shè)置文件類型為Word
intent.setDataAndType(uri, "application/msword");
} else{
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/msword");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
? 這里 mContext.getExternalCacheDir() 中獲取的路徑 Android\data\包名\cache 我需要的文件路徑為Android\data\包名\cache\docs\ 所以在 child 中設(shè)為 “docs“ 。
? 之后對(duì)Android系統(tǒng)版本進(jìn)行判斷,若>=7.0,則創(chuàng)建 content://Uri , 否則創(chuàng)建 file:///Uri。
? getUriForFile() 的三個(gè)參數(shù) 第一個(gè)context 無需多說、第二個(gè)參數(shù)為 包名 + .fileprovider , 第三個(gè)參數(shù)為之前創(chuàng)建的要打開的file。
為Uri生成暫時(shí)的Permissions
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
將Uri提供給其他程序
intent.setDataAndType(uri, "application/msword");
mContext.startActivity(intent);
官方文檔中介紹了以下6種不同的路徑以及對(duì)應(yīng)的函數(shù)
<files-path name="name" path="path" />
? 等價(jià)于使用 Context.getFilesDir()
<cache-path name="name" path="path" />
? 等價(jià)于使用 getCacheDir()
<external-path name="name" path="path" />
? 等價(jià)于使用 Environment.getExternalStorageDirectory().
<external-files-path name="name" path="path" />
等價(jià)于使用 `Context#getExternalFilesDir(String) Context.getExternalFilesDir(null)`.
<external-cache-path name="name" path="path" />
? 等價(jià)于使用 Context.getExternalCacheDir().
<external-media-path name="name" path="path" />
? 等價(jià)于使用 Context.getExternalMediaDirs(). 需要在api21+中使用