加载中...

4.4.2 ContentProvider再探——Document Provider


本节引言:

学完上一节,相信你已经知道如何去使用系统提供的ContentProvider或者自定义ContentProvider了, 已经基本满足日常开发的需求了,有趣的是,我在官方文档上看到了另外这几个Provider:

此处输入图片的描述

Calendar Provider:日历提供者,就是针对针对日历相关事件的一个资源库,通过他提供的API,我们 可以对日历,时间,会议,提醒等内容做一些增删改查!
Contacts Provider:联系人提供者,这个就不用说了,这个用得最多~后面有时间再回头翻译下这篇文章吧!
Storage Access Framework(SAF):存储访问框架,4.4以后引入的一个新玩意,为用户浏览手机中的 存储内容提供了便利,可供访问的内容不仅包括:文档,图片,视频,音频,下载,而且包含所有由 由特定ContentProvider(须具有约定的API)提供的内容。不管这些内容来自于哪里,不管是哪个应 用调用浏览系统文件内容的命令,系统都会用一个统一的界面让你去浏览。
其实就是一个内置的应用程序,叫做DocumentsUI,因为它的IntentFilter不带有LAUNCHER,所以我们并没有 在桌面上找到这个东东!嘿嘿,试下下面的代码,这里我们选了两个手机来对比: 分别是4.2的Lenovo S898T 和 5.0.1的Nexus 5做对比,执行下述代码:

  1. Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
  2. intent.addCategory(Intent.CATEGORY_OPENABLE);
  3. intent.setType("image/*");
  4. startActivity(intent);
下面是运行结果:

右面这个就是4.4给我们带来的新玩意了,一般我们获取文件Url的时候就可以用到它~ 接下来简单的走下文档吧~

2.简单走下文档:

1)SAF框架的组成:

  • Document provider:一个特殊的ContentProvider,让一个存储服务(比如Google Drive)可以 对外展示自己所管理的文件。它是DocumentsProvider的子类,另外,document-provider的存储格式 和传统的文件存储格式一致,至于你的内容如何存储,则完全决定于你自己,Android系统已经内置了几个 这样的Document provider,比如关于下载,图片以及视频的Document provider!
  • Client app:一个普通的客户端软件,通过触发ACTION_OPEN_DOCUMENT 和/或 ACTION_CREATE_DOCUMENT就可以接收到来自于Document provider返回的内容,比如选择一个图片, 然后返回一个Uri。
  • Picker:类似于文件管理器的界面,而且是系统级的界面,提供额访问客户端过滤条件的 Document provider内容的通道,就是起说的那个DocumentsUI程序!

一些特性:

  • 用户可以浏览所有document provider提供的内容,而不仅仅是单一的应用程序
  • 提供了长期、持续的访问document provider中文件的能力以及数据的持久化, 用户可以实现添加、删除、编辑、保存document provider所维护的内容
  • 支持多用户以及临时性的内容服务,比如USB storage providers只有当驱动安装成功才会出现

2)概述:

SAF的核心是实现了DocumentsProvider的子类,还是一个ContentProvider。在一个document provider 中是以传统的文件目录树组织起来的:

3)流程图:

如上面所述,document provider data是基于传统的文件层次结构的,不过那只是对外的表现形式, 如何存储你的数据,取决于你自己,只要你对海外的接口能够通过DocumentsProvider的api访问就可以。 下面的流程图展示了一个photo应用使用SAF可能的结构:

分析:

从上图,我们可以看出Picker是链接调用者和内容提供者的一个桥梁!他提供并告诉调用者,可以选择 哪些内容提供者,比如这里的DriveDocProvider,UsbDocProvider,CloundDocProvider。
当客户端触发了ACTION_OPEN_DOCUMENTACTION_CREATE_DOCUMENT的Intent,就会发生上述交互。 当然我们还可以在Intent中增加过滤条件,比如限制MIME type的类型为"image"!

就是上面这些东西,如果你还安装了其他看图的软件的话,也会在这里看到! 简单点说就是:客户端发送了上面两种Action的Intent后,会打开Picker UI,在这里会显示相关可用的 Document Provider,供用户选择,用户选择后可以获得文件的相关信息!

4)客户端调用,并获取返回的Uri

实现代码如下:

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. private static final int READ_REQUEST_CODE = 42;
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. Button btn_show = (Button) findViewById(R.id.btn_show);
  9. btn_show.setOnClickListener(this);
  10. }
  11.  
  12. @Override
  13. public void onClick(View v) {
  14. Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
  15. intent.addCategory(Intent.CATEGORY_OPENABLE);
  16. intent.setType("image/*");
  17. startActivityForResult(intent, READ_REQUEST_CODE);
  18. }
  19.  
  20. @Override
  21. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  22. if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
  23. Uri uri;
  24. if (data != null) {
  25. uri = data.getData();
  26. Log.e("HeHe", "Uri: " + uri.toString());
  27. }
  28. }
  29. }
  30. }

运行结果: 比如我们选中那只狗,然后Picker UI自己会关掉,然后Logcat上可以看到这样一个uri:

5)根据uri获取文件参数

核心代码如下:

  1. public void dumpImageMetaData(Uri uri) {
  2. Cursor cursor = getContentResolver()
  3. .query(uri, null, null, null, null, null);
  4. try {
  5. if (cursor != null && cursor.moveToFirst()) {
  6. String displayName = cursor.getString(
  7. cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
  8. Log.e("HeHe", "Display Name: " + displayName);
  9. int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
  10. String size = null;
  11. if (!cursor.isNull(sizeIndex)) {
  12. size = cursor.getString(sizeIndex);
  13. }else {
  14. size = "Unknown";
  15. }
  16. Log.e("HeHe", "Size: " + size);
  17. }
  18. }finally {
  19. cursor.close();
  20. }
  21. }

运行结果: 还是那只狗,调用方法后会输入文件名以及文件大小,以byte为单位

6)根据Uri获得Bitmap

核心代码如下:

  1. private Bitmap getBitmapFromUri(Uri uri) throws IOException {
  2. ParcelFileDescriptor parcelFileDescriptor =
  3. getContentResolver().openFileDescriptor(uri, "r");
  4. FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
  5. Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
  6. parcelFileDescriptor.close();
  7. return image;
  8. }

运行结果

7)根据Uri获取输入流

核心代码如下:

  1. private String readTextFromUri(Uri uri) throws IOException {
  2. InputStream inputStream = getContentResolver().openInputStream(uri);
  3. BufferedReader reader = new BufferedReader(new InputStreamReader(
  4. inputStream));
  5. StringBuilder stringBuilder = new StringBuilder();
  6. String line;
  7. while ((line = reader.readLine()) != null) {
  8. stringBuilder.append(line);
  9. }
  10. fileInputStream.close();
  11. parcelFileDescriptor.close();
  12. return stringBuilder.toString();
  13. }

上述的内容只告诉你通过一个Uri你可以知道什么,而Uri的获取则是通过SAF得到的!

8) 创建新文件以及删除文件:

创建文件:

  1. private void createFile(String mimeType, String fileName) {
  2. Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
  3. intent.addCategory(Intent.CATEGORY_OPENABLE);
  4. intent.setType(mimeType);
  5. intent.putExtra(Intent.EXTRA_TITLE, fileName);
  6. startActivityForResult(intent, WRITE_REQUEST_CODE);
  7. }

可在onActivityResult()中获取被创建文件的uri

删除文件:

前提是Document.COLUMN_FLAGS包含SUPPORTS_DELETE

  1. DocumentsContract.deleteDocument(getContentResolver(), uri);

9)编写一个自定义的Document Provider

如果你希望自己应用的数据也能在documentsui中打开,你就需要写一个自己的document provider。 下面介绍自定义DocumentsProvider的步骤:

  • API版本为19或者更高
  • 在manifest.xml中注册该Provider
  • Provider的name为类名加包名,比如: com.example.android.storageprovider.MyCloudProvider
  • Authority为包名+provider的类型名,如: com.example.android.storageprovider.documents
  • android:exported属性的值为ture

下面是Provider的例子写法:

  1. <manifest... >
  2. ...
  3. <uses-sdk
  4. android:minSdkVersion="19"
  5. android:targetSdkVersion="19" />
  6. ....
  7. <provider
  8. android:name="com.example.android.storageprovider.MyCloudProvider"
  9. android:authorities="com.example.android.storageprovider.documents"
  10. android:grantUriPermissions="true"
  11. android:exported="true"
  12. android:permission="android.permission.MANAGE_DOCUMENTS"
  13. android:enabled="@bool/atLeastKitKat">
  14. <intent-filter>
  15. <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
  16. </intent-filter>
  17. </provider>
  18. </application>
  19.  
  20. </manifest>

10 )DocumentsProvider的子类

至少实现如下几个方法:

  • queryRoots()
  • queryChildDocuments()
  • queryDocument()
  • openDocument()

还有些其他的方法,但并不是必须的。下面演示一个实现访问文件(file)系统的 DocumentsProvider的大致写法。

Implement queryRoots

  1. @Override
  2. public Cursor queryRoots(String[] projection) throws FileNotFoundException {
  3.  
  4. // Create a cursor with either the requested fields, or the default
  5. // projection if "projection" is null.
  6. final MatrixCursor result =
  7. new MatrixCursor(resolveRootProjection(projection));
  8.  
  9. // If user is not logged in, return an empty root cursor. This removes our
  10. // provider from the list entirely.
  11. if (!isUserLoggedIn()) {
  12. return result;
  13. }
  14.  
  15. // It's possible to have multiple roots (e.g. for multiple accounts in the
  16. // same app) -- just add multiple cursor rows.
  17. // Construct one row for a root called "MyCloud".
  18. final MatrixCursor.RowBuilder row = result.newRow();
  19. row.add(Root.COLUMN_ROOT_ID, ROOT);
  20. row.add(Root.COLUMN_SUMMARY, getContext().getString(R.string.root_summary));
  21.  
  22. // FLAG_SUPPORTS_CREATE means at least one directory under the root supports
  23. // creating documents. FLAG_SUPPORTS_RECENTS means your application's most
  24. // recently used documents will show up in the "Recents" category.
  25. // FLAG_SUPPORTS_SEARCH allows users to search all documents the application
  26. // shares.
  27. row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE |
  28. Root.FLAG_SUPPORTS_RECENTS |
  29. Root.FLAG_SUPPORTS_SEARCH);
  30.  
  31. // COLUMN_TITLE is the root title (e.g. Gallery, Drive).
  32. row.add(Root.COLUMN_TITLE, getContext().getString(R.string.title));
  33.  
  34. // This document id cannot change once it's shared.
  35. row.add(Root.COLUMN_DOCUMENT_ID, getDocIdForFile(mBaseDir));
  36.  
  37. // The child MIME types are used to filter the roots and only present to the
  38. // user roots that contain the desired type somewhere in their file hierarchy.
  39. row.add(Root.COLUMN_MIME_TYPES, getChildMimeTypes(mBaseDir));
  40. row.add(Root.COLUMN_AVAILABLE_BYTES, mBaseDir.getFreeSpace());
  41. row.add(Root.COLUMN_ICON, R.drawable.ic_launcher);
  42.  
  43. return result;
  44. }

Implement queryChildDocuments

  1. public Cursor queryChildDocuments(String parentDocumentId, String[] projection,
  2. String sortOrder) throws FileNotFoundException {
  3.  
  4. final MatrixCursor result = new
  5. MatrixCursor(resolveDocumentProjection(projection));
  6. final File parent = getFileForDocId(parentDocumentId);
  7. for (File file : parent.listFiles()) {
  8. // Adds the file's display name, MIME type, size, and so on.
  9. includeFile(result, null, file);
  10. }
  11. return result;
  12. }

Implement queryDocument

  1. @Override
  2. public Cursor queryDocument(String documentId, String[] projection) throws
  3. FileNotFoundException {
  4.  
  5. // Create a cursor with the requested projection, or the default projection.
  6. final MatrixCursor result = new
  7. MatrixCursor(resolveDocumentProjection(projection));
  8. includeFile(result, documentId, null);
  9. return result;
  10. }

好吧,文档中的内容大概就是这些了: 一开始是想自己翻译的,后来在泡在网上的日子上找到了这一篇文档的中文翻译,就偷下懒了~

中文翻译链接:android存储访问框架Storage Access Framework

3.Android 4.4 获取资源路径问题:

其实这个SAF我们用得较多的地方无非是获取图片的Uri而已,而从上面的例子我们也发现了: 我们这样获取的链接是这样的:

  1. content://com.android.providers.media.documents/document/image%3A69983

这样的链接,我们直接通过上面的方法获得uri即可!

当然,这个是4.4 或者以上版本的~!

如果是以前的版本:uri可能是这样的:

  1. content://media/external/images/media/image%3A69983

这里贴下在别的地方看到的一个全面的方案,原文链接:Android4.4中获取资源路径问题

  1. public static String getPath(final Context context, final Uri uri) {
  2. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  3. // DocumentProvider
  4. if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  5. // ExternalStorageProvider
  6. if (isExternalStorageDocument(uri)) {
  7. final String docId = DocumentsContract.getDocumentId(uri);
  8. final String[] split = docId.split(":");
  9. final String type = split[0];
  10.  
  11. if ("primary".equalsIgnoreCase(type)) {
  12. return Environment.getExternalStorageDirectory() + "/" + split[1];
  13. }
  14.  
  15. // TODO handle non-primary volumes
  16. }
  17. // DownloadsProvider
  18. else if (isDownloadsDocument(uri)) {
  19.  
  20. final String id = DocumentsContract.getDocumentId(uri);
  21. final Uri contentUri = ContentUris.withAppendedId(
  22. Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  23.  
  24. return getDataColumn(context, contentUri, null, null);
  25. }
  26. // MediaProvider
  27. else if (isMediaDocument(uri)) {
  28. final String docId = DocumentsContract.getDocumentId(uri);
  29. final String[] split = docId.split(":");
  30. final String type = split[0];
  31. Uri contentUri = null;
  32. if ("image".equals(type)) {
  33. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  34. } else if ("video".equals(type)) {
  35. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  36. } else if ("audio".equals(type)) {
  37. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  38. }
  39. final String selection = "_id=?";
  40. final String[] selectionArgs = new String[] {
  41. split[1]
  42. };
  43. return getDataColumn(context, contentUri, selection, selectionArgs);
  44. }
  45. }
  46. // MediaStore (and general)
  47. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  48. return getDataColumn(context, uri, null, null);
  49. }
  50. // File
  51. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  52. return uri.getPath();
  53. }
  54. return null;
  55. }
  56.  
  57. /**
  58. * Get the value of the data column for this Uri. This is useful for
  59. * MediaStore Uris, and other file-based ContentProviders.
  60. *
  61. * @param context The context.
  62. * @param uri The Uri to query.
  63. * @param selection (Optional) Filter used in the query.
  64. * @param selectionArgs (Optional) Selection arguments used in the query.
  65. * @return The value of the _data column, which is typically a file path.
  66. */
  67. public static String getDataColumn(Context context, Uri uri, String selection,
  68. String[] selectionArgs) {
  69.  
  70. Cursor cursor = null;
  71. final String column = "_data";
  72. final String[] projection = {
  73. column
  74. };
  75.  
  76. try {
  77. cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
  78. null);
  79. if (cursor != null && cursor.moveToFirst()) {
  80. final int column_index = cursor.getColumnIndexOrThrow(column);
  81. return cursor.getString(column_index);
  82. }
  83. } finally {
  84. if (cursor != null)
  85. cursor.close();
  86. }
  87. return null;
  88. }
  89.  
  90.  
  91. /**
  92. * @param uri The Uri to check.
  93. * @return Whether the Uri authority is ExternalStorageProvider.
  94. */
  95. public static boolean isExternalStorageDocument(Uri uri) {
  96. return "com.android.externalstorage.documents".equals(uri.getAuthority());
  97. }
  98.  
  99. /**
  100. * @param uri The Uri to check.
  101. * @return Whether the Uri authority is DownloadsProvider.
  102. */
  103. public static boolean isDownloadsDocument(Uri uri) {
  104. return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  105. }
  106.  
  107. /**
  108. * @param uri The Uri to check.
  109. * @return Whether the Uri authority is MediaProvider.
  110. */
  111. public static boolean isMediaDocument(Uri uri) {
  112. return "com.android.providers.media.documents".equals(uri.getAuthority());
  113. }

本节小结:

好的,关于本节android存储访问框架SAF就到这里吧,没什么例子,后面用到再深入研究吧, 知道下就好,4.4后获取文件路径就简单多了~


还没有评论.