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

相关内容

热门资讯

重大科普!闲逸亲友圈app辅助... 重大科普!闲逸亲友圈app辅助(辅助挂)wpk透视工作室(素来真的有挂);闲逸亲友圈app辅助软件透...
总算了解!来物局潮汕辅助(辅助... 总算了解!来物局潮汕辅助(辅助挂)德州局HHpoker透视脚本(原先有挂);总算了解!来物局潮汕辅助...
科普分享!微信边锋辅助(辅助挂... 科普分享!微信边锋辅助(辅助挂)wpk辅助是什么(竟然有挂);大家肯定在之前微信边锋辅助或者微信边锋...
一分钟带你了解!钱塘十水三攻略... 一分钟带你了解!钱塘十水三攻略(辅助挂)hhpoker真的假的(原先真的有挂);超受欢迎的钱塘十水三...
玩家必备攻略!蜜瓜大厅辅助免费... 玩家必备攻略!蜜瓜大厅辅助免费下载(辅助挂)wepoker正确养号方法(原先有挂);玩家必备攻略!蜜...
科普攻略!蜀山四川小程序脚本(... 《科普攻略!蜀山四川小程序脚本(辅助挂)hh poker辅助有用吗(都是是真的有挂)》 蜀山四川小程...
一分钟秒懂!先锋脚本辅助(辅助... 一分钟秒懂!先锋脚本辅助(辅助挂)wepoker钻石怎么看底牌(原生存在有挂);先锋脚本辅助中的10...
技术分享!点点游戏辅助(辅助挂... 技术分享!点点游戏辅助(辅助挂)sohoo poker辅助器(好像是有挂);详细点点游戏辅助攻略(点...
每日必看推荐!水鱼辅助软件下载... 每日必看推荐!水鱼辅助软件下载(辅助挂)佛手大菠萝13道挂哪里(从前是有挂)是一款可以让一直输的玩家...
每日必备!小程序微乐游戏辅助器... 每日必备!小程序微乐游戏辅助器(辅助挂)wepoker轻量版有透视吗(果然真的是有挂)是一款可以让一...