ContentProvider再探——Document Provider
概述
通過ContentProvider初探相信你已經(jīng)知道如何去使用系統(tǒng)提供的ContentProvider或者自定義ContentProvider了, 已經(jīng)基本滿足日常開發(fā)的需求了,有趣的是,我在官方文檔上看到了另外這幾個(gè)Provider:
- Calendar Provider:日歷提供者,就是針對(duì)針對(duì)日歷相關(guān)事件的一個(gè)資源庫(kù),通過他提供的API,我們 可以對(duì)日歷,時(shí)間,會(huì)議,提醒等內(nèi)容做一些增刪改查!
- Contacts Provider:聯(lián)系人提供者,這個(gè)就不用說了,這個(gè)用得最多
- Storage Access Framework(SAF):存儲(chǔ)訪問框架,4.4 API 19 以后引入的一個(gè)新玩意,為用戶瀏覽手機(jī)中的 存儲(chǔ)內(nèi)容提供了便利,可供訪問的內(nèi)容不僅包括:文檔,圖片,視頻,音頻,下載,而且包含所有由 由特定ContentProvider(須具有約定的API)提供的內(nèi)容。不管這些內(nèi)容來(lái)自于哪里,不管是哪個(gè)應(yīng) 用調(diào)用瀏覽系統(tǒng)文件內(nèi)容的命令,系統(tǒng)都會(huì)用一個(gè)統(tǒng)一的界面讓你去瀏覽。
預(yù)覽
我們選了兩個(gè)手機(jī)來(lái)對(duì)比: 分別是4.2的Lenovo S898T 和 5.0.1的Nexus 5做對(duì)比,執(zhí)行下述代碼:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setType("image/*");startActivity(intent);右面這個(gè)就是4.4以后給我們帶來(lái)的新玩意了,一般我們獲取文件Url的時(shí)候就可以用到它。
使用
1)SAF框架的組成:
- Document provider:一個(gè)特殊的ContentProvider,讓一個(gè)存儲(chǔ)服務(wù)(比如Google Drive)可以 對(duì)外展示自己所管理的文件。它是DocumentsProvider的子類,另外,document-provider的存儲(chǔ)格式 和傳統(tǒng)的文件存儲(chǔ)格式一致,至于你的內(nèi)容如何存儲(chǔ),則完全決定于你自己,Android系統(tǒng)已經(jīng)內(nèi)置了幾個(gè) 這樣的Document provider,比如關(guān)于下載,圖片以及視頻的Document provider!
- Client app:一個(gè)普通的客戶端軟件,通過觸發(fā)ACTION_OPEN_DOCUMENT 和/或 ACTION_CREATE_DOCUMENT就可以接收到來(lái)自于Document provider返回的內(nèi)容,比如選擇一個(gè)圖片, 然后返回一個(gè)Uri。
- Picker:類似于文件管理器的界面,而且是系統(tǒng)級(jí)的界面,提供額訪問客戶端過濾條件的 Document provider內(nèi)容的通道,就是起說的那個(gè)DocumentsUI程序!
一些特性:
- 用戶可以瀏覽所有document provider提供的內(nèi)容,而不僅僅是單一的應(yīng)用程序
- 提供了長(zhǎng)期、持續(xù)的訪問document provider中文件的能力以及數(shù)據(jù)的持久化, 用戶可以實(shí)現(xiàn)添加、刪除、編輯、保存document provider所維護(hù)的內(nèi)容
- 支持多用戶以及臨時(shí)性的內(nèi)容服務(wù),比如USB storage providers只有當(dāng)驅(qū)動(dòng)安裝成功才會(huì)出現(xiàn)
2)概述:
SAF的核心是實(shí)現(xiàn)了DocumentsProvider的子類,還是一個(gè)ContentProvider。在一個(gè)document provider 中是以傳統(tǒng)的文件目錄樹組織起來(lái)的:
3)流程圖:
如上面所述,document provider data是基于傳統(tǒng)的文件層次結(jié)構(gòu)的,不過那只是對(duì)外的表現(xiàn)形式, 如何存儲(chǔ)你的數(shù)據(jù),取決于你自己,只要你對(duì)外的接口能夠通過DocumentsProvider的api訪問就可以。 下面的流程圖展示了一個(gè)photo應(yīng)用使用SAF可能的結(jié)構(gòu):
分析:
從上圖,我們可以看出Picker是鏈接調(diào)用者和內(nèi)容提供者的一個(gè)橋梁!他提供并告訴調(diào)用者,可以選擇 哪些內(nèi)容提供者,比如這里的DriveDocProvider,UsbDocProvider,CloundDocProvider。
當(dāng)客戶端觸發(fā)了ACTION_OPEN_DOCUMENT或ACTION_CREATE_DOCUMENT的Intent,就會(huì)發(fā)生上述交互。 當(dāng)然我們還可以在Intent中增加過濾條件,比如限制MIME type的類型為”image”!
就是上面這些東西,如果你還安裝了其他看圖的軟件的話,也會(huì)在這里看到! 簡(jiǎn)單點(diǎn)說就是:客戶端發(fā)送了上面兩種Action的Intent后,會(huì)打開Picker UI,在這里會(huì)顯示相關(guān)可用的 Document Provider,供用戶選擇,用戶選擇后可以獲得文件的相關(guān)信息!
4)客戶端調(diào)用,并獲取返回的Uri
實(shí)現(xiàn)代碼如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {private static final int READ_REQUEST_CODE = 42;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn_show = (Button) findViewById(R.id.btn_show);btn_show.setOnClickListener(this);}@Overridepublic void onClick(View v) {Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setType("image/*");startActivityForResult(intent, READ_REQUEST_CODE);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {Uri uri;if (data != null) {uri = data.getData();Log.e("HeHe", "Uri: " + uri.toString());}}} }運(yùn)行結(jié)果: 比如我們選中那只狗,然后Picker UI自己會(huì)關(guān)掉,然后Logcat上可以看到這樣一個(gè)uri:
5)根據(jù)uri獲取文件參數(shù)
核心代碼如下:
public void dumpImageMetaData(Uri uri) {Cursor cursor = getContentResolver().query(uri, null, null, null, null, null);try {if (cursor != null && cursor.moveToFirst()) {String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));Log.e("HeHe", "Display Name: " + displayName);int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);String size = null;if (!cursor.isNull(sizeIndex)) {size = cursor.getString(sizeIndex);}else {size = "Unknown";}Log.e("HeHe", "Size: " + size);}}finally {cursor.close();} }運(yùn)行結(jié)果: 還是那只狗,調(diào)用方法后會(huì)輸入文件名以及文件大小,以byte為單位
6)根據(jù)Uri獲得Bitmap
private Bitmap getBitmapFromUri(Uri uri) throws IOException {ParcelFileDescriptor parcelFileDescriptor =getContentResolver().openFileDescriptor(uri, "r");FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);parcelFileDescriptor.close();return image; }7)根據(jù)Uri獲取輸入流
private String readTextFromUri(Uri uri) throws IOException {InputStream inputStream = getContentResolver().openInputStream(uri);BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));StringBuilder stringBuilder = new StringBuilder();String line;while ((line = reader.readLine()) != null) {stringBuilder.append(line);}fileInputStream.close();parcelFileDescriptor.close();return stringBuilder.toString(); }上述的內(nèi)容只告訴你通過一個(gè)Uri你可以知道什么,而Uri的獲取則是通過SAF得到的!
8) 創(chuàng)建新文件以及刪除文件:
創(chuàng)建文件:
private void createFile(String mimeType, String fileName) {Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setType(mimeType);intent.putExtra(Intent.EXTRA_TITLE, fileName);startActivityForResult(intent, WRITE_REQUEST_CODE); }可在onActivityResult()中獲取被創(chuàng)建文件的uri
刪除文件:
前提是Document.COLUMN_FLAGS包含SUPPORTS_DELETE
DocumentsContract.deleteDocument(getContentResolver(), uri);9)編寫一個(gè)自定義的Document Provider
如果你希望自己應(yīng)用的數(shù)據(jù)也能在documentsui中打開,你就需要寫一個(gè)自己的document provider。 下面介紹自定義DocumentsProvider的步驟:
- API版本為19或者更高
- 在manifest.xml中注冊(cè)該P(yáng)rovider
- Provider的name為類名加包名,比如: com.example.android.storageprovider.MyCloudProvider
- Authority為包名+provider的類型名,如: com.example.android.storageprovider.documents
- android:exported屬性的值為ture
下面是Provider的例子寫法:
<manifest... >...<uses-sdkandroid:minSdkVersion="19"android:targetSdkVersion="19" />....<providerandroid:name="com.example.android.storageprovider.MyCloudProvider"android:authorities="com.example.android.storageprovider.documents"android:grantUriPermissions="true"android:exported="true"android:permission="android.permission.MANAGE_DOCUMENTS"android:enabled="@bool/atLeastKitKat"><intent-filter><action android:name="android.content.action.DOCUMENTS_PROVIDER" /></intent-filter></provider></application></manifest>10 )DocumentsProvider的子類
至少實(shí)現(xiàn)如下幾個(gè)方法
- queryRoots()
- queryChildDocuments()
- queryDocument()
- openDocument()
還有些其他的方法,但并不是必須的。下面演示一個(gè)實(shí)現(xiàn)訪問文件(file)系統(tǒng)的 DocumentsProvider的大致寫法。
Implement queryRoots
@Override public Cursor queryRoots(String[] projection) throws FileNotFoundException {// Create a cursor with either the requested fields, or the default// projection if "projection" is null.final MatrixCursor result =new MatrixCursor(resolveRootProjection(projection));// If user is not logged in, return an empty root cursor. This removes our// provider from the list entirely.if (!isUserLoggedIn()) {return result;}// It's possible to have multiple roots (e.g. for multiple accounts in the// same app) -- just add multiple cursor rows.// Construct one row for a root called "MyCloud".final MatrixCursor.RowBuilder row = result.newRow();row.add(Root.COLUMN_ROOT_ID, ROOT);row.add(Root.COLUMN_SUMMARY, getContext().getString(R.string.root_summary));// FLAG_SUPPORTS_CREATE means at least one directory under the root supports// creating documents. FLAG_SUPPORTS_RECENTS means your application's most// recently used documents will show up in the "Recents" category.// FLAG_SUPPORTS_SEARCH allows users to search all documents the application// shares.row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE |Root.FLAG_SUPPORTS_RECENTS |Root.FLAG_SUPPORTS_SEARCH);// COLUMN_TITLE is the root title (e.g. Gallery, Drive).row.add(Root.COLUMN_TITLE, getContext().getString(R.string.title));// This document id cannot change once it's shared.row.add(Root.COLUMN_DOCUMENT_ID, getDocIdForFile(mBaseDir));// The child MIME types are used to filter the roots and only present to the// user roots that contain the desired type somewhere in their file hierarchy.row.add(Root.COLUMN_MIME_TYPES, getChildMimeTypes(mBaseDir));row.add(Root.COLUMN_AVAILABLE_BYTES, mBaseDir.getFreeSpace());row.add(Root.COLUMN_ICON, R.drawable.ic_launcher);return result; }Implement queryChildDocuments
public Cursor queryChildDocuments(String parentDocumentId, String[] projection,String sortOrder) throws FileNotFoundException {final MatrixCursor result = newMatrixCursor(resolveDocumentProjection(projection));final File parent = getFileForDocId(parentDocumentId);for (File file : parent.listFiles()) {// Adds the file's display name, MIME type, size, and so on.includeFile(result, null, file);}return result; }Implement queryDocument
@Override public Cursor queryDocument(String documentId, String[] projection) throwsFileNotFoundException {// Create a cursor with the requested projection, or the default projection.final MatrixCursor result = newMatrixCursor(resolveDocumentProjection(projection));includeFile(result, documentId, null);return result; }Android 4.4 獲取資源路徑問題
其實(shí)這個(gè)SAF我們用得較多的地方無(wú)非是獲取圖片的Uri而已,而從上面的例子我們也發(fā)現(xiàn)了: 我們這樣獲取的鏈接是這樣的:
content://com.android.providers.media.documents/document/image%3A69983這樣的鏈接,我們直接通過上面的方法獲得uri即可!
當(dāng)然,這個(gè)是4.4 或者以上版本的~!
如果是以前的版本:uri可能是這樣的:
別人總結(jié)的Android4.4中獲取資源路徑問題
public static String getPath(final Context context, final Uri uri) {final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;// DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {// ExternalStorageProvider if (isExternalStorageDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];if ("primary".equalsIgnoreCase(type)) {return Environment.getExternalStorageDirectory() + "/" + split[1];}// TODO handle non-primary volumes }// DownloadsProvider else if (isDownloadsDocument(uri)) {final String id = DocumentsContract.getDocumentId(uri);final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));return getDataColumn(context, contentUri, null, null);}// MediaProvider else if (isMediaDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];Uri contentUri = null;if ("image".equals(type)) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if ("video".equals(type)) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if ("audio".equals(type)) {contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;}final String selection = "_id=?";final String[] selectionArgs = new String[] {split[1]};return getDataColumn(context, contentUri, selection, selectionArgs);}}// MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) {return getDataColumn(context, uri, null, null);}// File else if ("file".equalsIgnoreCase(uri.getScheme())) {return uri.getPath();}return null; }/*** Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. ** @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection,String[] selectionArgs) {Cursor cursor = null;final String column = "_data";final String[] projection = {column};try {cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null);if (cursor != null && cursor.moveToFirst()) {final int column_index = cursor.getColumnIndexOrThrow(column);return cursor.getString(column_index);}} finally {if (cursor != null)cursor.close();}return null; }/*** @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) {return "com.android.externalstorage.documents".equals(uri.getAuthority()); }/*** @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) {return "com.android.providers.downloads.documents".equals(uri.getAuthority()); }/*** @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) {return "com.android.providers.media.documents".equals(uri.getAuthority()); }總結(jié)
以上是生活随笔為你收集整理的ContentProvider再探——Document Provider的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ContentProvider初探
- 下一篇: Intent传递数据全解