展开菜单
首页 精品内容 本月促销 装机必备 Windows PDF教程 macOS软件 IOS软件 Android AI 专题
全部分类

当前位置:

首页 > 系统应用 > 4.4.2 ContentProvider再探——Document Provider

4.4.2 ContentProvider再探——Document Provider

Process Lasso 专业版
Process Lasso 专业版

Process Lasso是一款功能Windows进程管理工具,可有效避免电脑出现蓝屏、假死等情况。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。

¥59
Windows 2026-09-24
正版软件 Windows Process 系统优化工具 专业版 进程管理
立即下载

本节引言: 学完上一节,相信你已经知道如何去使用系统提供的ContentProvider或者自定义ContentProvider了, 已经基本满足日常开发的需求了,有趣的是,我在官方文档上看到了另外这几个Provider: Calendar 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做对比,执行下述代码:

     Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");
            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_DOCUMENT或ACTION_CREATE_DOCUMENT的Intent,就会发生上述交互。 当然我们还可以在Intent中增加过滤条件,比如限制MIME type的类型为"image"!

    上面提到的这些内容,都会出现在这里;如果设备里还装了别的看图软件,同样也会一起显示出来。说得再直白一点:当客户端发送前面这两种 Action 对应的 Intent 之后,系统就会拉起 Picker UI,把当前可用的 Document Provider 展示给用户挑选;等用户选定之后,也就能拿到对应文件的相关信息了。

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

    实现代码如下:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private static final int READ_REQUEST_CODE = 42;
    @Override
        protected void onCreate(Bundle sa vedInstanceState) {
            super.onCreate(sa vedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn_show = (Button) findViewById(R.id.btn_show);
            btn_show.setOnClickListener(this);
        }
    @Override
        public 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);
        }
    @Override
        protected 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());
                }
            }
        }
    }

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

    5)根据uri获取文件参数

    核心代码如下:

    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();
        }
    }

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

    6)根据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)根据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();
    }

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

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

    创建文件:

    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()中获取被创建文件的uri

    删除文件:

    前提是Document.COLUMN_FLAGS包含SUPPORTS_DELETE

    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的例子写法:

    
        ...
        
            ....
            
                
                    
                
            
        
    

    10 )DocumentsProvider的子类

    至少实现如下几个方法:

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

    还有些其他的方法,但并不是必须的。下面演示一个实现访问文件(file)系统的 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 ha ve 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_A VAILABLE_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 = new
                MatrixCursor(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) throws
            FileNotFoundException {
    // Create a cursor with the requested projection, or the default projection.
        final MatrixCursor result = new
                MatrixCursor(resolveDocumentProjection(projection));
        includeFile(result, documentId, null);
        return result;
    }

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

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

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

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

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

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

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

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

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

    这里贴下在别的地方看到的一个全面的方案,原文链接: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());
    }

    本节小结:

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

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
    作者最新文章
    系统应用 Pro
    相关文章 更多
    精品专题 更多
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    PDF教程

    正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

    macOS软件

    正软商城macOS软件专区,精选适用于Mac电脑的办公、设计、影音、效率、开发和系统工具,提供软件功能介绍、macOS兼容版本、正版授权及购买下载服务。

    IOS软件

    正软商城iOS软件专区,精选适用于iPhone和iPad的办公、学习、影音、设计、效率及AI应用,提供功能介绍、适用设备、系统要求和正版获取方式等信息。

    AI

    正软商城AI软件专区,汇集AI写作、AI绘画、AI视频、AI办公、AI编程、AI翻译、智能客服和数据分析等人工智能工具,提供功能介绍、适用平台、收费方式及正版购买信息。

    Mac软件 更多
    Aperty macOS版
    Aperty macOS版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw macOS版
    AionClaw macOS版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy macOS版
    Loomy macOS版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    AirBuddy 3 macOS版
    AirBuddy 3 macOS版

    AirBuddy 3 是面向 macOS 的蓝牙外设增强工具,可集中查看 AirPods、Beats 与苹果设备电量,提供快速连接、播放控制、电量提醒、桌面小组件和多台 Mac 外设接力等功能。

    AirServer macOS版
    AirServer macOS版

    AirServer macOS版可把 Mac 转换为无线投屏接收端,通过 AirPlay 或 Google Cast 接收手机、平板和电脑画面,并支持多设备同时连接、高清镜像及会话录制,适合家庭娱乐、课堂教学、会议演示和应用展示。

    AnyDesk macOS版
    AnyDesk macOS版

    AnyDesk macOS版是一款适用于远程办公、设备维护和客户支持的远程桌面工具,可连接并操作异地计算机,支持文件传输、无人值守访问、会话权限控制及跨平台协作。

    WINDOWS 更多
    讯飞绘文 网页版
    讯飞绘文 网页版

    讯飞绘文网页版是科大讯飞推出的AI图文创作与内容运营工具,覆盖热点选题、文章生成、稿件校对、智能配图、排版发布和数据复盘等环节,适合自媒体创作者及内容团队在线使用。

    Maya(Autodesk Maya) Windows版
    Maya(Autodesk Maya) Windows版

    Autodesk Maya 是面向影视、电视、动画和游戏行业的专业三维制作软件,集多边形与 NURBS 建模、角色绑定、关键帧和动作捕捉动画、Bifrost 特效模拟、材质开发及 Arnold 渲染于一体,

    Aperty Windows版
    Aperty Windows版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw Windows版
    AionClaw Windows版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy Windows版
    Loomy Windows版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    AirServer Windows版
    AirServer Windows版

    AirServer Windows版可把电脑变成AirPlay、Google Cast和Miracast无线投屏接收端,适合会议演示、课堂教学及家庭大屏分享。发送设备通常无需另装客户端,并可同时接收和智能排列多个镜像会话。

    即将离开本站
    您即将前往第三方网站,请确认是否继续?