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

相关内容

热门资讯

透视数据"德普之星怎... 透视数据"德普之星怎么设置埋牌"详细辅助存在挂教程(到底是挂了辅助);原来确实真的有挂(需添加指定薇...
辅助透视"wepok... 辅助透视"wepokerh5破解"详细辅助必赢教程(辅助工具技巧);人气非常高,ai更新快且高清可以...
透视中牌率"htx矩... 透视中牌率"htx矩阵wepoker辅助"详细辅助透视教程(私人局透视技巧)是一款可以让一直输的玩家...
透视能赢"wepok... 透视能赢"wepoker科技辅助器"详细辅助AA德州教程(辅助器透明);玩家必备必赢加哟《13670...
透视插件"wejok... 透视插件"wejoker辅助软件"详细辅助教你攻略(游戏下载透视)1、很好的工具软件,可以解锁游戏的...
透视智能ai"wep... 这是一款非常优秀的wepoker怎么看底牌 ia辅助检测软件,能够让你了解到wepoker怎么看底牌...
透视最新"hhpok... 透视最新"hhpoker透视工具"详细辅助透明教程(插件功能辅助器方法);小薇(透视辅助)致您一封信...
透视安装"wepok... 1、透视安装"wepoker有辅助功能吗"详细辅助AA德州教程(高级辅助透明)。2、wepoker有...
透视软件"wepok... 透视软件"wepoker手机版辅助"详细辅助AI教程(可以增加运气辅助);支持2-10人实时对战,虚...
透视存在"黑侠破解w... 1、透视存在"黑侠破解wepoker"详细辅助可靠教程(辅助器v3.3教程)2、进入游戏-大厅左侧-...