Android如何实现一个DocumentProvider示例详解
作者:storytellerF 时间:2022-12-30 20:10:13
前言
假如你做了一个云盘类的app,或者可以保存用户导入的配置。用户在未来肯定需要获取这些文件,一个办法是写一个Activity,向一个文件管理软件一样把他们列出来。但是这个有一个问题是用户必须进入app 才能访问。
现在有一个解决方案是实现一个DocumentProvider
步骤
DocumentProvider 继承自Content Provider。
首先在Manifest 中注册这个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>
创建这个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
在我们的需求下只有一种情况,访问的路径应该是/data/data/packagename/,不过如果一个app 支持多用户,那就应该有多个目录。所以需要一个方法告知文件管理应用我们的app 有多少个根。
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 就像一个数据库连接查询数据时一样,不过我们没有操作数据库,返回的数据是文件系统。所以我们返回一个MatrixCursor。
返回的数据没有什么真实数据,仅仅作为一个入口。
此时打开Android 系统的文件管理,就能看到了
此时点击的话就会崩溃,因为还没有重写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;
这个方法才是真正返回数据的地方,上面返回root 更像是盘符,这里是返回root 盘符对应的根文件夹。数据结构是一个树形结构,queryDocument 返回根文件夹,根只有一个,所以返回的数据也是只有一个。如果你返回了多个数据应该也是无效的,系统会忽略掉。
一般我们需要根据documentId (盘符)返回,不过这里只有一个,就不做区分了。
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)
}
}
我们只需要根据parentDocumentId 来搜索就可以。就像一个遍历出一个文件夹的子文件夹和子文件一样。
除了上面介绍的,还可以继承打开document,创建document,显示document thumbnail 等功能。
代码可以在这里看 github.com/storyteller…
来源:https://juejin.cn/post/7180568940174082106
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现屏幕旋转方法总结
Flutter Widgets之标签类控件Chip详解
![](https://img.aspxhome.com/file/2023/0/120090_0s.jpg)
c++ 预处理的图灵完备之引言
Java实现堆排序和图解
![](https://img.aspxhome.com/file/2023/6/59336_0s.png)
Android实现清除单个域名的cookie
Android O添加桌面快捷方式的示例
简单的一次springMVC路由跳转实现
![](https://img.aspxhome.com/file/2023/6/110896_0s.png)
java使用集合实现通讯录功能
Java ynchronized重量级锁的核心原理详解
![](https://img.aspxhome.com/file/2023/3/89503_0s.png)
Java日常练习题,每天进步一点点(7)
![](https://img.aspxhome.com/file/2023/8/59978_0s.png)
Android定时器实现的几种方式整理及removeCallbacks失效问题解决
![](https://img.aspxhome.com/file/2023/9/137159_0s.gif)
idea搭建SSM框架遇踩的坑(附完整过程)
![](https://img.aspxhome.com/file/2023/6/66506_0s.png)
Android 调试工具用法详细介绍
Java使用线程同步解决线程安全问题详解
![](https://img.aspxhome.com/file/2023/9/78029_0s.png)
解析maven的用法和几个常用的命令(推荐)
![](https://img.aspxhome.com/file/2023/4/87174_0s.jpg)
专属于程序员的浪漫-Java输出动态闪图iloveyou
![](https://img.aspxhome.com/file/2023/8/84608_0s.png)
淘宝IP地址库采集器c#代码
![](https://img.aspxhome.com/file/2023/6/77266_0s.png)
一篇文章带你入门Java数据类型
![](https://img.aspxhome.com/file/2023/8/62338_0s.jpg)
SpringBoot事件发布和监听详解
![](https://img.aspxhome.com/file/2023/9/108439_0s.png)