AndroidStudio:如何从intent.Action_Open_Document_tree获取存储路径?
创始人
2024-08-21 13:00:44
0

要获取从“打开文档树”意图中选择的存储位置的路径,可以使用以下代码示例:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == PICK_DOC_REQUEST && resultCode == RESULT_OK && intent.getData() != null) {
        Uri uriTree = intent.getData();

        // Check for the "DocumentProvider" authority
        if (DocumentsContract.isDocumentUri(this, uriTree)) {
            // The document provider was selected
            String docId = DocumentsContract.getTreeDocumentId(uriTree);

            // Get complete Uri of this document by adding document id to the tree Uri
            Uri uri = DocumentsContract.buildDocumentUriUsingTree(uriTree, docId);

            // Get details about the selected directory, including its document id and display name
            String [] projection = {DocumentsContract.Document.COLUMN_DOCUMENT_ID,
                                    DocumentsContract.Document.COLUMN_DISPLAY_NAME};
            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

            if (cursor != null && cursor.moveToFirst()) {
                String folderDocumentId = cursor.getString(0);
                String folderName = cursor.getString(1);

                // Use folderDocumentId and folderName
                Log.d(TAG, "Folder Document Id: " + folderDocumentId);
                Log.d(TAG, "Folder Name: " + folderName);
            }

            if (cursor != null) {
                cursor.close();
            }
        }
    } else {
        super.onActivityResult(requestCode, resultCode, intent);
    }
}

其中, PICK_DOC_REQUEST是一个用于标识请求代码的整数常量,应该在启动 ACTION_OPEN_DOCUMENT_TREE 意图时使用。

此代码将检查获取的 Uri 是否属于“文件提供者”,获取所选目录的详细信息,并从中提取必要的信息。此时,您可以使用 folderDocumentId 来获取完整的 Uri 并进行其他操作。

相关内容

热门资讯

第七分钟外挂!真人天天途游破解... 第七分钟外挂!真人天天途游破解安装包,蘑菇云辅助使用视频,必赢方法(确实有挂)-哔哩哔哩1、超多福利...
第五分钟了解!开心泉州小程序开... 第五分钟了解!开心泉州小程序开挂有什么技巧,决战卡五星辅助看牌器(竟然是真的挂)-哔哩哔哩在进入开心...
第1分钟外挂!福州十八扑插件,... 第1分钟外挂!福州十八扑插件,浙江宝宝游戏辅助器,系统教程(今日头条)-哔哩哔哩1、浙江宝宝游戏辅助...
五分钟揭幕!衢州都莱辅助器是真... 五分钟揭幕!衢州都莱辅助器是真是假,榕城510k辅助软件(果然存在有挂)-哔哩哔哩1、衢州都莱辅助器...
四分钟外挂!瓜瓜丰城双剑旧版攻... 四分钟外挂!瓜瓜丰城双剑旧版攻略,欢聚水鱼脚本,微扑克教程(有挂工具)-哔哩哔哩1、完成瓜瓜丰城双剑...
第三分钟专业!约战竞技场辅助器... 第三分钟专业!约战竞技场辅助器,点星休闲修改(好像存在有挂)-哔哩哔哩1、任何约战竞技场辅助器ai辅...
第二分钟解迷!闲逸辅助功能怎么... 第二分钟解迷!闲逸辅助功能怎么下载,微友三代辅助(本来真的是有挂)-哔哩哔哩1、很好的工具软件,可以...
第六分钟外挂!游戏辅助神器ap... 第六分钟外挂!游戏辅助神器app,衢州都莱辅助器下载,详细教程(发现有挂)-哔哩哔哩1、第六分钟外挂...
第七分钟详情!潮汕馆插件,海盗... 第七分钟详情!潮汕馆插件,海盗来了刷能量辅助(好像真的是有挂)-哔哩哔哩1、游戏颠覆性的策略玩法,独...
第5分钟外挂!雀友会广东潮汕辅... 第5分钟外挂!雀友会广东潮汕辅助脚本,九酷众游辅助,存在挂教程(有挂方略)-哔哩哔哩1、下载好雀友会...