FileProvider 介紹
FileProvider 是 ContentProvider 的一個特殊子類尖淘,通過以 content://
代替 file:///
Uri 來安全地分享與app關(guān)聯(lián)的文件奕锌。
”content URI“允許你給授予臨時的讀寫訪問權(quán)限。
當(dāng)你創(chuàng)建一個包含”content URI“的 Intent 時村生,想要將它發(fā)送給另一個app你需要調(diào)用Intent.setFlags()
添加權(quán)限惊暴。Intent被發(fā)到Activity時,這些權(quán)限在此Activity在棧中活動期間可用趁桃。Intent被發(fā)到Service時辽话,這些權(quán)限在此Service運(yùn)行期間可用。
作為對比镇辉,使用file:///
Uri 控制訪問屡穗,你必須修改文件的文件系統(tǒng)權(quán)限。你提供的權(quán)限對所有app可用并保持有效直到你改變它們忽肛。這種級別的訪問從根本上是不安全的。
”content URI“提供的文件訪問安全性使FileProvider成為Android安全基礎(chǔ)設(shè)施的一個關(guān)鍵部分烂斋。
指定對外暴露的目錄
創(chuàng)建一個xml文件并在其中指定對外暴露的目錄屹逛。
路徑節(jié)點包含以下兩個屬性:
- name础废,URI路徑片段。用于分享時隱藏真實路徑罕模。
- path评腺,待分享的子目錄。留空表示該類路徑的根目錄淑掌。
有多種路徑節(jié)點類型:
-
files-path蒿讥,應(yīng)用內(nèi)部存儲的files子目錄,對應(yīng)Context.getFilesDir()抛腕,例如
/data/user/0/${applicationId}/files
-
cache-path芋绸,應(yīng)用內(nèi)部存儲的cache子目錄,對應(yīng)Context.getCacheDir()担敌,例如
/data/user/0/${applicationId}/cache
-
external-files-path摔敛,應(yīng)用外部存儲的files子目錄,對應(yīng)Context.getExternalFilesDir(String)全封,例如
/storage/emulated/0/Android/data/${applicationId}/files
-
external-cache-path马昙,應(yīng)用外部存儲的cache子目錄,對應(yīng)Context.getExternalCacheDir()刹悴,例如
/storage/emulated/0/Android/data/${applicationId}/cache
-
external-path行楞,外部存儲根目錄,對應(yīng)Environment.getExternalStorageDirectory()土匀,例如
/storage/emulated/0
- root-path敢伸,系統(tǒng)根目錄"/"
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琢歇,設(shè)為true岁疼,表示允許授予臨時共享權(quán)限卓嫂。
<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>
分享授予了臨時權(quán)限的文件
分享應(yīng)用files目錄下images子目錄的文件default_image.jpg
File file = new File(getExternalCacheDir(), "images/default_image.jpg");
Uri contentUri = FileProvider.getUriForFile(this, "com.mydomain.fileprovider", file);
給Uri授予臨時權(quán)限宙暇,有 讀/寫 兩個權(quán)限外遇,可以單獨或同時授予
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);
發(fā)送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