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
猜你喜欢
- @EqualsAndHashCode注解@EqualsAndHashCode注解的作用就是自动实现model类的equals方法和hashc
- 题目描述这是 LeetCode 上的 768. 最多能完成排序的块 II ,难度为 困难。Tag : 「贪心」这个问题和&ldquo
- 介绍该系统有三个角色,分别是:普通用户、房屋中介、管理员。普通用户的功能:浏览房屋信息、预约看房、和中介聊天、申请成为中介等等。房屋中介的功
- 近几天又温习了一下SpringMVC的运行机制以及原理我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)
- Activity中Toast的使用Toast.makeText(this,"ADD",Toast.LENGTH_SHOR
- 1. 算法分析根据概率将奖品划分区间,每个区间代表一个奖品,然后抽取 随机数,反查落在那个区间上,即为所抽取的奖品。2. 代码核心
- 前言在开始本文之前要说明以下,首先我是一个初学springmvc,抱着去加深印象的目的去整理相关springmvc4的相关注解,同时也希望给
- 表单的重复提交: 没有完整的进行一次,先请求表单页面->再提交表单过程而完成数据提交造成的根本原因: 没有完整的进行一次,先请求表单页
- 后台服务端import java.io.IOException;import java.io.InputStream;import java
- 【引用】迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径。 它的主要特点是以起始点为中心
- Android的WebView做不到ios的WebView那样可以很方便的直接预览pdf文件。要实现利用WebView预览pdf我们可以使用
- 说到人脸检测,首先要了解Haar特征分类器。Haar特征分类器说白了就是一个个的xml文件,不同的xml里面描述人体各个部位的特征值,比如人
- 最近跳槽去新公司,接受的第一个任务是在 一个电商模块的搜索功能以及搜索历史记录的实现。需求和淘宝等电商的功能大体差不多,最上面一个搜索框,下
- 一、短信登录验证机制原理分析了解短信验证码的登陆机制之前,我们首先是要了解用户账号密码登陆的机制是如何的,我们来简要分析一下Spring S
- 本文实例为大家分享了Java实现医院管理系统的具体代码,供大家参考,具体内容如下1.开发工具NetBeans8.2Mysql5.7mysql
- 一、前言在做Java项目开发过程中,涉及到一些数据库服务连接配置、缓存服务器连接配置等,通常情况下我们会将这些不太变动的配置信息存储在以 .
- 什么是 Spring Boot 插件?Spring Boot 插件是一种扩展机制,它提供了一种简单的方式来扩展 Spring Boot 的功
- 本文实例为大家分享了C#实现剪刀石头布游戏的具体代码,供大家参考,具体内容如下游戏界面如下所示:首先我们必须知道要创建三个类玩家类,电脑类,
- 此次简单的操作将数据从数据库导出生成excel报表以及将excel数据导入数据库首先建立数据库的连接池:package jdbc;impor
- 在spring boot中,简单几步,使用spring AOP实现一个 * :1、引入依赖:<dependency> &nbs