分享文件
编写:jdneo - 原文:<http://developer.android.com/training/secure-file-sharing/sharing-file.html>
对应用程序进行配置,使得它可以使用Content URI来共享文件后,其就可以响应其他应用程序的获取文件的请求了。一种响应这些请求的方法是在服务端应用程序提供一个可以由其他应用激活的文件选择接口。该方法可以允许客户端应用程序让用户从服务端应用程序选择一个文件,然后接收这个文件的Content URI。
本课将会展示如何在应用中创建一个用于选择文件的Activity,来响应这些获取文件的请求。
接收文件请求
为了从客户端应用程序接收一个文件获取请求并以Content URI的形式进行响应,我们的应用程序应该提供一个选择文件的Activity。客户端应用程序通过调用startActivityForResult()方法启动这一Activity。该方法包含了一个具有ACTION_PICKAction的Intent参数。当客户端应用程序调用了startActivityForResult(),我们的应用可以向客户端应用程序返回一个结果,该结果即用户所选择的文件所对应的Content URI。
关于如何在客户端应用程序实现文件获取请求,请参考:请求分享一个文件。
创建一个选择文件的Activity
为建立一个选择文件的Activity,首先需要在Manifest清单文件中定义Activity,在其Intent过滤器中,匹配ACTION_PICKAction及CATEGORY_DEFAULT和CATEGORY_OPENABLE这两种Category。另外,还需要为应用程序设置MIME类型过滤器,来表明我们的应用程序可以向其他应用程序提供哪种类型的文件。下面这段代码展示了如何在清单文件中定义新的Activity和Intent过滤器:
1. <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2. ...
3. <application>
4. ...
5. <activity
6. android:name=".FileSelectActivity"
7. android:label="File Selector" >
8. <intent-filter>
9. <action
10. android:name="android.intent.action.PICK"/>
11. <category
12. android:name="android.intent.category.DEFAULT"/>
13. <category
14. android:name="android.intent.category.OPENABLE"/>
15. <data android:mimeType="text/plain"/>
16. <data android:mimeType="image/*"/>
17. </intent-filter>
18. </activity>
在代码中定义文件选择Activity
下面,定义一个Activity子类,用于显示在内部存储的“files/images/”目录下可以获得的文件,然后允许用户选择期望的文件。下面代码展示了如何定义该Activity,并令其响应用户的选择:
1. public class MainActivity extends Activity {
2. // The path to the root of this app's internal storage
3. private File mPrivateRootDir;
4. // The path to the "images" subdirectory
5. private File mImagesDir;
6. // Array of files in the images subdirectory
7. File[] mImageFiles;
8. // Array of filenames corresponding to mImageFiles
9. String[] mImageFilenames;
10. // Initialize the Activity
11. @Override
12. protected void onCreate(Bundle savedInstanceState) {
13. ...
14. // Set up an Intent to send back to apps that request a file
15. mResultIntent =
16. new Intent("com.example.myapp.ACTION_RETURN_FILE");
17. // Get the files/ subdirectory of internal storage
18. mPrivateRootDir = getFilesDir();
19. // Get the files/images subdirectory;
20. mImagesDir = new File(mPrivateRootDir, "images");
21. // Get the files in the images subdirectory
22. mImageFiles = mImagesDir.listFiles();
23. // Set the Activity's result to null to begin with
24. setResult(Activity.RESULT_CANCELED, null);
25. /*
26. * Display the file names in the ListView mFileListView.
27. * Back the ListView with the array mImageFilenames, which
28. * you can create by iterating through mImageFiles and
29. * calling File.getAbsolutePath() for each File
30. */
31. ...
32. }
33. ...
34. }
响应一个文件选择
一旦用户选择了一个被共享的文件,我们的应用程序必须明确哪个文件被选择了,并为该文件生成一个对应的Content URI。如果我们的Activity在ListView中显示了可获得文件的清单,那么当用户点击了一个文件名时,系统会调用方法onItemClick(),在该方法中可以获取被选择的文件。
在onItemClick()中,根据被选中文件的文件名获取一个File对象,然后将其作为参数传递给getUriForFile(),另外还需传入的参数是在<provider>标签中为FileProvider所指定的Authority,函数返回的Content URI包含了相应的Authority,一个对应于文件目录的路径标记(如在XML meta-data中定义的),以及包含扩展名的文件名。有关FileProvider如何基于XML meta-data将目录路径与路径标记进行匹配的知识,可以阅读:指定可共享目录路径。
下例展示了如何检测选中的文件并且获得它的Content URI:
1. protected void onCreate(Bundle savedInstanceState) {
2. ...
3. // Define a listener that responds to clicks on a file in the ListView
4. mFileListView.setOnItemClickListener(
5. new AdapterView.OnItemClickListener() {
6. @Override
7. /*
8. * When a filename in the ListView is clicked, get its
9. * content URI and send it to the requesting app
10. */
11. public void onItemClick(AdapterView<?> adapterView,
12. View view,
13. int position,
14. long rowId) {
15. /*
16. * Get a File for the selected file name.
17. * Assume that the file names are in the
18. * mImageFilename array.
19. */
20. File requestFile = new File(mImageFilename[position]);
21. /*
22. * Most file-related method calls need to be in
23. * try-catch blocks.
24. */
25. // Use the FileProvider to get a content URI
26. try {
27. fileUri = FileProvider.getUriForFile(
28. MainActivity.this,
29. "com.example.myapp.fileprovider",
30. requestFile);
31. } catch (IllegalArgumentException e) {
32. Log.e("File Selector",
33. "The selected file can't be shared: " +
34. clickedFilename);
35. }
36. ...
37. }
38. });
39. ...
40. }
记住,我们能生成的那些Content URI所对应的文件,必须是那些在meta-data文件中包含<paths>标签的目录内的文件,这方面知识在Specify Sharable Directories中已经讨论过。如果调用getUriForFile()方法所要获取的文件不在我们指定的目录中,会收到一个IllegalArgumentException。
为文件授权
现在已经有了想要共享给其他应用程序的文件所对应的Content URI,我们需要允许客户端应用程序访问这个文件。为了达到这一目的,可以通过将Content URI添加至一个Intent中,然后为该Intent设置权限标记。所授予的权限是临时的,并且当接收文件的应用程序的任务栈终止后,会自动过期。
下例展示了如何为文件设置读权限:
1. protected void onCreate(Bundle savedInstanceState) {
2. ...
3. // Define a listener that responds to clicks in the ListView
4. mFileListView.setOnItemClickListener(
5. new AdapterView.OnItemClickListener() {
6. @Override
7. public void onItemClick(AdapterView<?> adapterView,
8. View view,
9. int position,
10. long rowId) {
11. ...
12. if (fileUri != null) {
13. // Grant temporary read permission to the content URI
14. mResultIntent.addFlags(
15. Intent.FLAG_GRANT_READ_URI_PERMISSION);
16. }
17. ...
18. }
19. ...
20. });
21. ...
22. }
Caution:调用setFlags()来为文件授予临时被访问权限是唯一的安全的方法。尽量避免对文件的Content URI调用Context.grantUriPermission(),因为通过该方法授予的权限,只能通过调用Context.revokeUriPermission()来撤销。
与请求应用共享文件
为了向请求文件的应用程序提供其需要的文件,我们将包含了Content URI和相应权限的Intent传递给setResult()。当定义的Activity结束后,系统会把这个包含了Content URI的Intent传递给客户端应用程序。下例展示了其中的核心步骤:
1. protected void onCreate(Bundle savedInstanceState) {
2. ...
3. // Define a listener that responds to clicks on a file in the ListView
4. mFileListView.setOnItemClickListener(
5. new AdapterView.OnItemClickListener() {
6. @Override
7. public void onItemClick(AdapterView<?> adapterView,
8. View view,
9. int position,
10. long rowId) {
11. ...
12. if (fileUri != null) {
13. ...
14. // Put the Uri and MIME type in the result Intent
15. mResultIntent.setDataAndType(
16. fileUri,
17. getContentResolver().getType(fileUri));
18. // Set the result
19. MainActivity.this.setResult(Activity.RESULT_OK,
20. mResultIntent);
21. } else {
22. mResultIntent.setDataAndType(null, "");
23. MainActivity.this.setResult(RESULT_CANCELED,
24. mResultIntent);
25. }
26. }
27. });
当用户选择好文件后,我们应该向用户提供一个能够立即回到客户端应用程序的方法。一种实现的方法是向用户提供一个勾选框或者一个完成按钮。可以使用按钮的android:onClick属性字段为它关联一个方法。在该方法中,调用finish()。例如:
1. public void onDoneClick(View v) {
2. // Associate a method with the Done button
3. finish();
4. }
