編寫(xiě):jdneo - 原文:http://developer.android.com/training/secure-file-sharing/request-file.html
當(dāng)一個(gè)應(yīng)用程序希望訪問(wèn)由其它應(yīng)用程序所共享的文件時(shí),請(qǐng)求應(yīng)用程序(客戶(hù)端)經(jīng)常會(huì)向其它應(yīng)用程序(服務(wù)端)發(fā)送一個(gè)文件請(qǐng)求。多數(shù)情況下,該請(qǐng)求會(huì)導(dǎo)致在服務(wù)端應(yīng)用程序中啟動(dòng)一個(gè)Activity,該Activity中會(huì)顯示可以共享的文件。當(dāng)服務(wù)端應(yīng)用程序向客戶(hù)端應(yīng)用程序返回了文件的Content URI后,用戶(hù)即可開(kāi)始選擇文件。
本課將展示一個(gè)客戶(hù)端應(yīng)用程序應(yīng)該如何向服務(wù)端應(yīng)用程序請(qǐng)求一個(gè)文件,接收服務(wù)端應(yīng)用程序發(fā)來(lái)的Content URI,然后使用這個(gè)Content URI打開(kāi)這個(gè)文件。
為了向服務(wù)端應(yīng)用程序發(fā)送文件請(qǐng)求,在客戶(hù)端應(yīng)用程序中,需要調(diào)用[startActivityForResult](http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int))方法,同時(shí)傳遞給這個(gè)方法一個(gè)Intent參數(shù),它包含了客戶(hù)端應(yīng)用程序能處理的某個(gè)Action,比如ACTION_PICK及一個(gè)MIME類(lèi)型。
例如,下面的代碼展示了如何向服務(wù)端應(yīng)用程序發(fā)送一個(gè)Intent,來(lái)啟動(dòng)在分享文件中提到的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);
...
}
...
}
當(dāng)服務(wù)端應(yīng)用程序向客戶(hù)端應(yīng)用程序發(fā)回包含Content URI的Intent時(shí),該Intent會(huì)傳遞給客戶(hù)端應(yīng)用程序重寫(xiě)的onActivityResult()方法當(dāng)中。一旦客戶(hù)端應(yīng)用程序擁有了文件的Content URI,它就可以通過(guò)獲取其FileDescriptor訪問(wèn)文件了。
這一過(guò)程中不用過(guò)多擔(dān)心文件的安全問(wèn)題,因?yàn)榭蛻?hù)端應(yīng)用程序所收到的所有數(shù)據(jù)只有文件的Content URI而已。由于URI不包含目錄路徑信息,客戶(hù)端應(yīng)用程序無(wú)法查詢(xún)或打開(kāi)任何服務(wù)端應(yīng)用程序的其他文件。客戶(hù)端應(yīng)用程序僅僅獲取了這個(gè)文件的訪問(wèn)渠道以及由服務(wù)端應(yīng)用程序授予的訪問(wèn)權(quán)限。同時(shí)訪問(wèn)權(quán)限是臨時(shí)的,一旦這個(gè)客戶(hù)端應(yīng)用的任務(wù)棧結(jié)束了,這個(gè)文件將無(wú)法再被除服務(wù)端應(yīng)用程序之外的其他應(yīng)用程序訪問(wèn)。
下面的例子展示了客戶(hù)端應(yīng)用程序應(yīng)該如何處理發(fā)自服務(wù)端應(yīng)用程序的Intent,以及客戶(hù)端應(yīng)用程序如何使用Content 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()方法返回一個(gè)文件的ParcelFileDescriptor對(duì)象??蛻?hù)端應(yīng)用程序從該對(duì)象中獲取FileDescriptor對(duì)象,然后利用該對(duì)象讀取這個(gè)文件了。