FileProvider 介紹
FileProvider 是 ContentProvider 的一個特殊子類,通過以 content://
代替 file:///
Uri 來安全地分享與app關聯的文件。
”content URI“允許你給授予臨時的讀寫訪問權限。
當你創建一個包含”content URI“的 Intent 時,想要將它發送給另一個app你需要調用Intent.setFlags()
添加權限。Intent被發到Activity時,這些權限在此Activity在棧中活動期間可用。Intent被發到Service時,這些權限在此Service運行期間可用。
作為對比,使用file:///
Uri 控制訪問,你必須修改文件的文件系統權限。你提供的權限對所有app可用并保持有效直到你改變它們。這種級別的訪問從根本上是不安全的。
”content URI“提供的文件訪問安全性使FileProvider成為Android安全基礎設施的一個關鍵部分。
指定對外暴露的目錄
創建一個xml文件并在其中指定對外暴露的目錄。
路徑節點包含以下兩個屬性:
- name,URI路徑片段。用于分享時隱藏真實路徑。
- path,待分享的子目錄。留空表示該類路徑的根目錄。
有多種路徑節點類型:
-
files-path,應用內部存儲的files子目錄,對應Context.getFilesDir(),例如
/data/user/0/${applicationId}/files
-
cache-path,應用內部存儲的cache子目錄,對應Context.getCacheDir(),例如
/data/user/0/${applicationId}/cache
-
external-files-path,應用外部存儲的files子目錄,對應Context.getExternalFilesDir(String),例如
/storage/emulated/0/Android/data/${applicationId}/files
-
external-cache-path,應用外部存儲的cache子目錄,對應Context.getExternalCacheDir(),例如
/storage/emulated/0/Android/data/${applicationId}/cache
-
external-path,外部存儲根目錄,對應Environment.getExternalStorageDirectory(),例如
/storage/emulated/0
- root-path,系統根目錄"/"
xml/file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!-- content://${applicationId}/app_internal_files/ -->
<!-- file:///data/user/0/${applicationId}/files/example/ -->
<files-path name="app_internal_files" path="example" />
<!-- content://${applicationId}/app_internal_cache/ -->
<!-- file:///data/user/0/${applicationId}/cache/example/ -->
<cache-path name="app_internal_cache" path="example" />
<!-- content://${applicationId}/app_external_files/ -->
<!-- file:///storage/emulated/0/Android/data/${applicationId}/files/example/ -->
<external-files-path name="app_external_files" path="example" />
<!-- content://${applicationId}/app_external_cache/ -->
<!-- file:///storage/emulated/0/Android/data/${applicationId}/cache/example/ -->
<external-cache-path name="app_external_cache" path="example" />
<!-- content://${applicationId}/external_root/ -->
<!-- file:///storage/emulated/0/example/ -->
<external-path name="external_root" path="example" />
<!-- content://${applicationId}/system_root/ -->
<!-- file:///example/ -->
<root-path name="system_root" path="example" />
<external-cache-path name="images" path="images" />
</paths>
在Manifest.xml中聲明FileProvider
聲明FileProvider并添加android:name為android.support.FILE_PROVIDER_PATHS
的<meta-data/>
- android:name,通常用v4包提供的FileProvider,也可以用自定義的。
-
android:authorities,通常格式為
${applicationId}.${yourprovider}
。 - android:exported,false,表示不需要對外開放。
- android:grantUriPermissions,設為true,表示允許授予臨時共享權限。
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
...
</application>
</manifest>
分享授予了臨時權限的文件
分享應用files目錄下images子目錄的文件default_image.jpg
File file = new File(getExternalCacheDir(), "images/default_image.jpg");
Uri contentUri = FileProvider.getUriForFile(this, "com.mydomain.fileprovider", file);
給Uri授予臨時權限,有 讀/寫 兩個權限,可以單獨或同時授予
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
發送Intent
startActivityForResult(intent, REQUEST_TAKE_PHOTO);
參考
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
https://developer.android.com/training/camera/photobasics.html