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

相关内容

热门资讯

第八分钟辅助!德州机器人代打脚... 第八分钟辅助!德州机器人代打脚本,xpoker辅助助手,总结教程(有挂方略)德州机器人代打脚本透视方...
三分钟辅助!wepoker私人... 三分钟辅助!wepoker私人局外卦,wpk透视是真的吗,指引教程(有挂方式)1、wepoker私人...
第1分钟辅助!wepoker私... 第1分钟辅助!wepoker私人局可以透视,aapoker如何设置胜率,资料教程(有挂方式)第1分钟...
五分钟辅助!hhpoker软件... 五分钟辅助!hhpoker软件安装包,wepoker好友房开挂,教材教程(果真有挂)wepoker好...
第2分钟辅助!wepoker透... 第2分钟辅助!wepoker透视底牌脚本,wepokerplus作弊,手筋教程(有人有挂)1)wep...
5分钟辅助!hhpoker俱乐... 5分钟辅助!hhpoker俱乐部是干嘛的,德扑圈有透视吗,手册教程(有挂规律)1、起透看视 hhpo...
第八分钟辅助!德州局透视脚本,... 第八分钟辅助!德州局透视脚本,聚星ai辅助工具收费多少,举措教程(真的有挂)1、游戏颠覆性的策略玩法...
第4分钟辅助!哈糖大菠萝有挂吗... 第4分钟辅助!哈糖大菠萝有挂吗5个常用方法,hhpoker透视工具,窍门教程(有挂技术)1、进入游戏...
第十分钟辅助!HH平台挂,黑侠... 第十分钟辅助!HH平台挂,黑侠破解wepoker,指引教程(有挂透视)黑侠破解wepoker透视方法...
第4分钟辅助!扑克之星辅助,h... 第4分钟辅助!扑克之星辅助,hhpoker德州有挂吗,窍要教程(有挂秘诀)1、第4分钟辅助!扑克之星...