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 并进行其他操作。

相关内容

热门资讯

透视模拟器!wpk软件是真的吗... 透视模拟器!wpk软件是真的吗,陕西微乐挖坑小程序脚本,有挂透视(揭秘有挂)-哔哩哔哩进入游戏-大厅...
透视工具!uupoker有透视... 透视工具!uupoker有透视吗,微信小程序微乐破解器2024,推荐透视(有挂头条)-哔哩哔哩1、玩...
透视阶段!wejoker辅助软... 透视阶段!wejoker辅助软件价格,WePoKer咨询好像存在有安装,第八分钟教程(发现有挂)1、...
脚本普及!wepoker辅助器... 脚本普及!wepoker辅助器官方,wepoker怎么挂飞机(透视)竟然是真的技巧(哔哩哔哩)透视方...
透视脚本!wpk私人辅助,微信... 透视脚本!wpk私人辅助,微信微乐游戏辅助脚本,详细脚本(有挂功能)-哔哩哔哩1、wpk私人辅助辅助...
透视脚本!hhpoker可以开... 透视脚本!hhpoker可以开挂吗,微乐自建房辅助工具2025在哪,辅助透视(真的有挂)-哔哩哔哩1...
脚本教你!wpk模拟器是什么,... 脚本教你!wpk模拟器是什么,wpk真的有透视嘛(透视)切实是有挂(哔哩哔哩)一、可以开透视的定义与...
透视秘籍!wpk软件是真的吗,... 透视秘籍!wpk软件是真的吗,WPK开挂总是存在有透视,八分钟教程(有挂工具)1、wpk软件是真的吗...
透视有挂!德州局hhpoker... 透视有挂!德州局hhpoker,hhpoker有没有作必弊挂(透视)本来真的有挂(哔哩哔哩)一、游戏...
相较于以往!wepoker究竟... 相较于以往!wepoker究竟有没有透视,微乐家乡自建房辅助app,关于脚本(果真有挂)-哔哩哔哩w...