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

相关内容

热门资讯

辅助透视!wepoker辅助视... 辅助透视!wepoker辅助视频,hhpoker辅助码怎么用,指南教程(本来是有挂)-哔哩哔哩1、h...
透视plus!pokemmo脚... 透视plus!pokemmo脚本(透视)开挂辅助软件(原来真的有挂)-哔哩哔哩1、pokemmo脚本...
透视ai代打!约局吧德州可以透... 透视ai代打!约局吧德州可以透视吗,hardrock辅助,烘培教程(都是存在有挂)-哔哩哔哩运约局吧...
透视机器人!拱趴大菠萝辅助方法... 透视机器人!拱趴大菠萝辅助方法(透视)开挂辅助脚本(果然真的有挂)-哔哩哔哩1、玩家可以在拱趴大菠萝...
透视脚本!hhpoker开挂教... 透视脚本!hhpoker开挂教程,wepoker免费钻石,办法教程(一贯是有挂)-哔哩哔哩1)hhp...
透视安卓版!hhpoker透视... 透视安卓版!hhpoker透视脚本(透视)开挂辅助app(一直真的是有挂)-哔哩哔哩1、全新机制【h...
透视黑科技!hardrock辅... 透视黑科技!hardrock辅助,wpk私人局有透视吗,积累教程(确实存在有挂)-哔哩哔哩1、游戏颠...
透视辅助器!hhpoker有没... 透视辅助器!hhpoker有没有辅助(透视)开挂辅助器(竟然真的是有挂)-哔哩哔哩1、完成hhpok...
透视教程!hhpoker开挂教... 透视教程!hhpoker开挂教程,佛手大菠萝辅助,模块教程(切实真的有挂)-哔哩哔哩1、在佛手大菠萝...
透视系统!竞技联盟辅助插件(透... 透视系统!竞技联盟辅助插件(透视)开挂辅助脚本(好像是有挂)-哔哩哔哩1、上手简单,内置详细流程视频...