请求分享一个文件

编写:jdneo - 原文:<http://developer.android.com/training/secure-file-sharing/request-file.html>

当一个应用程序希望访问由其它应用程序所共享的文件时,请求应用程序(客户端)经常会向其它应用程序(服务端)发送一个文件请求。多数情况下,该请求会导致在服务端应用程序中启动一个Activity,该Activity中会显示可以共享的文件。当服务端应用程序向客户端应用程序返回了文件的Content URI后,用户即可开始选择文件。

本课将展示一个客户端应用程序应该如何向服务端应用程序请求一个文件,接收服务端应用程序发来的Content URI,然后使用这个Content URI打开这个文件。

发送一个文件请求

为了向服务端应用程序发送文件请求,在客户端应用程序中,需要调用startActivityForResult方法,同时传递给这个方法一个Intent参数,它包含了客户端应用程序能处理的某个Action,比如ACTION_PICK及一个MIME类型。

例如,下面的代码展示了如何向服务端应用程序发送一个Intent,来启动在分享文件中提到的Activity:

1. public class MainActivity extends Activity {
2. private Intent mRequestFileIntent;
3. private ParcelFileDescriptor mInputPFD;
4. ...
5. @Override
6. protected void onCreate(Bundle savedInstanceState) {
7. super.onCreate(savedInstanceState);
8. setContentView(R.layout.activity_main);
9. mRequestFileIntent = new Intent(Intent.ACTION_PICK);
10. mRequestFileIntent.setType("image/jpg");
11. ...
12. }
13. ...
14. protected void requestFile() {
15. /**
16. * When the user requests a file, send an Intent to the
17. * server app.
18. * files.
19. */
20. startActivityForResult(mRequestFileIntent, 0);
21. ...
22. }
23. ...
24. }

访问请求的文件

当服务端应用程序向客户端应用程序发回包含Content URI的Intent时,该Intent会传递给客户端应用程序重写的onActivityResult()方法当中。一旦客户端应用程序拥有了文件的Content URI,它就可以通过获取其FileDescriptor访问文件了。

这一过程中不用过多担心文件的安全问题,因为客户端应用程序所收到的所有数据只有文件的Content URI而已。由于URI不包含目录路径信息,客户端应用程序无法查询或打开任何服务端应用程序的其他文件。客户端应用程序仅仅获取了这个文件的访问渠道以及由服务端应用程序授予的访问权限。同时访问权限是临时的,一旦这个客户端应用的任务栈结束了,这个文件将无法再被除服务端应用程序之外的其他应用程序访问。

下面的例子展示了客户端应用程序应该如何处理发自服务端应用程序的Intent,以及客户端应用程序如何使用Content URI获取FileDescriptor:

1. /*
2. * When the Activity of the app that hosts files sets a result and calls
3. * finish(), this method is invoked. The returned Intent contains the
4. * content URI of a selected file. The result code indicates if the
5. * selection worked or not.
6. */
7. @Override
8. public void onActivityResult(int requestCode, int resultCode,
9. Intent returnIntent) {
10. // If the selection didn't work
11. if (resultCode != RESULT_OK) {
12. // Exit without doing anything else
13. return;
14. } else {
15. // Get the file's content URI from the incoming Intent
16. Uri returnUri = returnIntent.getData();
17. /*
18. * Try to open the file for "read" access using the
19. * returned URI. If the file isn't found, write to the
20. * error log and return.
21. */
22. try {
23. /*
24. * Get the content resolver instance for this context, and use it
25. * to get a ParcelFileDescriptor for the file.
26. */
27. mInputPFD = getContentResolver().openFileDescriptor(returnUri, "r");
28. } catch (FileNotFoundException e) {
29. e.printStackTrace();
30. Log.e("MainActivity", "File not found.");
31. return;
32. }
33. // Get a regular file descriptor for the file
34. FileDescriptor fd = mInputPFD.getFileDescriptor();
35. ...
36. }
37. }

openFileDescriptor()方法返回一个文件的ParcelFileDescriptor对象。客户端应用程序从该对象中获取FileDescriptor对象,然后利用该对象读取这个文件了。