最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android如何實(shí)現(xiàn)一個(gè)DocumentProvider示例詳解

 更新時(shí)間:2022年12月25日 16:55:40   作者:storytellerF  
這篇文章主要為大家介紹了Android如何實(shí)現(xiàn)一個(gè)DocumentProvider示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

假如你做了一個(gè)云盤類的app,或者可以保存用戶導(dǎo)入的配置。用戶在未來肯定需要獲取這些文件,一個(gè)辦法是寫一個(gè)Activity,向一個(gè)文件管理軟件一樣把他們列出來。但是這個(gè)有一個(gè)問題是用戶必須進(jìn)入app 才能訪問。

現(xiàn)在有一個(gè)解決方案是實(shí)現(xiàn)一個(gè)DocumentProvider

步驟

DocumentProvider 繼承自Content Provider。

首先在Manifest 中注冊(cè)這個(gè)Provider

<provider
    android:name=".StorageProvider"
    android:authorities="com.storyteller_f.ping.documents"
    android:grantUriPermissions="true"
    android:exported="true"
    android:permission="android.permission.MANAGE_DOCUMENTS">
    <intent-filter>
        <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
    </intent-filter>
</provider>

創(chuàng)建這個(gè)Provider

class StorageProvider : DocumentsProvider() {
    companion object {
        private val DEFAULT_ROOT_PROJECTION: Array<String> = arrayOf(
            DocumentsContract.Root.COLUMN_ROOT_ID,
            DocumentsContract.Root.COLUMN_MIME_TYPES,
            DocumentsContract.Root.COLUMN_FLAGS,
            DocumentsContract.Root.COLUMN_ICON,
            DocumentsContract.Root.COLUMN_TITLE,
            DocumentsContract.Root.COLUMN_SUMMARY,
            DocumentsContract.Root.COLUMN_DOCUMENT_ID,
            DocumentsContract.Root.COLUMN_AVAILABLE_BYTES
        )
        private val DEFAULT_DOCUMENT_PROJECTION: Array<String> = arrayOf(
            DocumentsContract.Document.COLUMN_DOCUMENT_ID,
            DocumentsContract.Document.COLUMN_MIME_TYPE,
            DocumentsContract.Document.COLUMN_FLAGS,
            DocumentsContract.Document.COLUMN_DISPLAY_NAME,
            DocumentsContract.Document.COLUMN_LAST_MODIFIED,
            DocumentsContract.Document.COLUMN_SIZE
        )
        private const val TAG = "StorageProvider"
    }
    override fun onCreate(): Boolean {
        return true
    }
}

重寫queryRoot

在我們的需求下只有一種情況,訪問的路徑應(yīng)該是/data/data/packagename/,不過如果一個(gè)app 支持多用戶,那就應(yīng)該有多個(gè)目錄。所以需要一個(gè)方法告知文件管理應(yīng)用我們的app 有多少個(gè)根。

override fun queryRoots(projection: Array<out String>?): Cursor {
    Log.d(TAG, "queryRoots() called with: projection = $projection")
    val flags = DocumentsContract.Root.FLAG_LOCAL_ONLY or DocumentsContract.Root.FLAG_SUPPORTS_IS_CHILD
    return MatrixCursor(projection ?: DEFAULT_ROOT_PROJECTION).apply {
        newRow().apply {
            add(DocumentsContract.Root.COLUMN_ROOT_ID, DEFAULT_ROOT_ID)
            add(DocumentsContract.Root.COLUMN_MIME_TYPES, DocumentsContract.Document.MIME_TYPE_DIR)
            add(DocumentsContract.Root.COLUMN_FLAGS, flags)
            add(DocumentsContract.Root.COLUMN_ICON, R.drawable.ic_launcher_foreground)
            add(DocumentsContract.Root.COLUMN_TITLE, context?.getString(R.string.app_name))
            add(DocumentsContract.Root.COLUMN_SUMMARY, "your data")
            add(DocumentsContract.Root.COLUMN_DOCUMENT_ID, "/")
        }
    }
}

返回值是Cursor 就像一個(gè)數(shù)據(jù)庫(kù)連接查詢數(shù)據(jù)時(shí)一樣,不過我們沒有操作數(shù)據(jù)庫(kù),返回的數(shù)據(jù)是文件系統(tǒng)。所以我們返回一個(gè)MatrixCursor。

返回的數(shù)據(jù)沒有什么真實(shí)數(shù)據(jù),僅僅作為一個(gè)入口。

此時(shí)打開Android 系統(tǒng)的文件管理,就能看到了

此時(shí)點(diǎn)擊的話就會(huì)崩潰,因?yàn)檫€沒有重寫queryDocument

重寫queryDocument

/**
 * Return metadata for the single requested document. You should avoid
 * making network requests to keep this request fast.
 *
 * @param documentId the document to return.
 * @param projection list of {@link Document} columns to put into the
 *            cursor. If {@code null} all supported columns should be
 *            included.
 * @throws AuthenticationRequiredException If authentication is required from
 *            the user (such as login credentials), but it is not guaranteed
 *            that the client will handle this properly.
 */
public abstract Cursor queryDocument(String documentId, String[] projection)
        throws FileNotFoundException;        

這個(gè)方法才是真正返回?cái)?shù)據(jù)的地方,上面返回root 更像是盤符,這里是返回root 盤符對(duì)應(yīng)的根文件夾。數(shù)據(jù)結(jié)構(gòu)是一個(gè)樹形結(jié)構(gòu),queryDocument 返回根文件夾,根只有一個(gè),所以返回的數(shù)據(jù)也是只有一個(gè)。如果你返回了多個(gè)數(shù)據(jù)應(yīng)該也是無效的,系統(tǒng)會(huì)忽略掉。

一般我們需要根據(jù)documentId (盤符)返回,不過這里只有一個(gè),就不做區(qū)分了。

override fun queryDocument(documentId: String?, projection: Array<out String>?): Cursor {
    Log.d(TAG, "queryDocument() called with: documentId = $documentId, projection = $projection")
    return MatrixCursor(projection ?: DEFAULT_DOCUMENT_PROJECTION).apply {
        val root = context?.filesDir?.parentFile ?: return@apply
        newRow().apply {
            add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, "/")
            add(DocumentsContract.Document.COLUMN_MIME_TYPE, DocumentsContract.Document.MIME_TYPE_DIR)
            val flags = 0
            add(DocumentsContract.Document.COLUMN_FLAGS, flags)
            add(DocumentsContract.Document.COLUMN_DISPLAY_NAME, root.name)
            add(DocumentsContract.Document.COLUMN_LAST_MODIFIED, root.lastModified())
            add(DocumentsContract.Document.COLUMN_SIZE, 0)
        }
    }
}

重寫getChildDocument

override fun queryChildDocuments(parentDocumentId: String?, projection: Array<out String>?, sortOrder: String?): Cursor {
    Log.d(TAG, "queryChildDocuments() called with: parentDocumentId = $parentDocumentId, projection = $projection, sortOrder = $sortOrder")
    return MatrixCursor(projection ?: DEFAULT_DOCUMENT_PROJECTION).apply {
        handleChild(parentDocumentId)
    }
}

我們只需要根據(jù)parentDocumentId 來搜索就可以。就像一個(gè)遍歷出一個(gè)文件夾的子文件夾和子文件一樣。

除了上面介紹的,還可以繼承打開document,創(chuàng)建document,顯示document thumbnail 等功能。

代碼可以在這里看 github.com/storyteller…

以上就是Android如何實(shí)現(xiàn)一個(gè)DocumentProvider示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Android實(shí)現(xiàn)DocumentProvider的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

金川县| 苏尼特右旗| 洪江市| 阳谷县| 砚山县| 平遥县| 梅河口市| 华容县| 泾川县| 潜山县| 义马市| 醴陵市| 大丰市| 通河县| 探索| 秀山| 衡山县| 靖宇县| 日喀则市| 庄浪县| 湘乡市| 营口市| 沿河| 新宁县| 贡山| 佛山市| 民和| 湖南省| 中卫市| 灌云县| 陆丰市| 兴安盟| 屏山县| 岫岩| 邵武市| 新龙县| 乳源| 辽阳市| 福鼎市| 祥云县| 肃南|