注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術一般,由于喜愛安卓而產生了翻譯的念頭,純屬個人興趣愛好。
原文鏈接: http://developer.android.com/training/secure-file-sharing/request-file.html
當一個希望訪問由其它應用所共享的文件時,需求應用(即客戶端)經常會向其它應用(服務端)發送一個文件需求。在大多數情況下,這個需求會在服務端應用啟動一個 Activity 顯示可以共享的文件。當服務應用向客戶應用返回了URI后,用戶即選擇了文件。
這節課向你展示一個客戶應用如何向服務應用需求一個文件,接受服務應用發來的URI,然后使用這個URI打開這個文件。
一). 發送一個文件需求
為了向服務應用發送文件需求,在客戶應用,需要調用
startActivityForResult
,同時傳遞給這個方法一個
Intent
,它包含了客戶應用能處理的某行為,比如
ACTION_PICK
;以及一個MIME類型。
例如,下面的代碼展示了如何向服務端應用發送一個
Intent
,來啟動在
Sharing a File
(博客鏈接:
http://www.cnblogs.com/jdneo/p/3480677.html
)中提到的
Activity
:
public class MainActivity extends Activity { private Intent mRequestFileIntent; private ParcelFileDescriptor mInputPFD; ... @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRequestFileIntent = new Intent(Intent.ACTION_PICK); mRequestFileIntent.setType( "image/jpg" ); ... } ... protected void requestFile() { /** * When the user requests a file, send an Intent to the * server app. * files. */ startActivityForResult(mRequestFileIntent, 0 ); ... } ... }
二). 訪問需求的文件 ?
當服務應用向客戶應用發回包含URI的 Intent 時,這個 Intent 會傳遞給客戶應用的覆寫的 onActivityResult() 方法當中。一旦客戶應用有了文件的URI,它就可以通過獲取其 FileDescriptor 來訪問文件。
文件的安全問題在這一過程中不用過多擔心,因為這個客戶應用所受到的所有數據只有URI而已。由于URI不包含目錄路徑,客戶應用無法查詢出或者打開任何服務應用的其他文件。客戶應用僅僅獲取了這個文件的訪問渠道和訪問的權限。同時訪問權限是臨時的,一旦這個客戶應用的任務棧被完成了,這個文件將只能被服務應用訪問。
下面的例子展示了客戶應用如何處理發自服務應用的
Intent
,以及如何客戶應用使用URI獲取
FileDescriptor
:
/* * When the Activity of the app that hosts files sets a result and calls * finish(), this method is invoked. The returned Intent contains the * content URI of a selected file. The result code indicates if the * selection worked or not. */ @Override public void onActivityResult( int requestCode, int resultCode, Intent returnIntent) { // If the selection didn't work if (resultCode != RESULT_OK) { // Exit without doing anything else return ; } else { // Get the file's content URI from the incoming Intent Uri returnUri = returnIntent.getData(); /* * Try to open the file for "read" access using the * returned URI. If the file isn't found, write to the * error log and return. */ try { /* * Get the content resolver instance for this context, and use it * to get a ParcelFileDescriptor for the file. */ mInputPFD = getContentResolver().openFileDescriptor(returnUri, "r" ); } catch (FileNotFoundException e) { e.printStackTrace(); Log.e( "MainActivity", "File not found." ); return ; } // Get a regular file descriptor for the file FileDescriptor fd = mInputPFD.getFileDescriptor(); ... } }
方法 openFileDescriptor() 返回一個文件的 ParcelFileDescriptor 。從這個對象中,客戶應用可以獲取 FileDescriptor 對象,然后用戶就可以利用這個對象讀取這個文件。 ?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
