简体   繁体   English

从内容URI获取目录路径

[英]Get directory path from content URI

I'm using the SDK's ACTION_OPEN_DOCUMENT_TREE intent to let the user select a directory. 我正在使用SDK的ACTION_OPEN_DOCUMENT_TREE意图让用户选择一个目录。 Here's the code: 这是代码:

private static final int REQUEST_PICK_FOLDER = 1;

// …

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_add_folder: {
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
            startActivityForResult(intent, REQUEST_PICK_FOLDER);
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_PICK_FOLDER && resultCode == Activity.RESULT_OK) {
        addFolder(data.getData());
    }
}

private void addFolder(Uri uri) {
    Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri, DocumentsContract.getTreeDocumentId(uri));
    Log.d(TAG, "Folder added: " + docUri.toString());
}

I have an URI (probably) pointing to the folder I just selected. 我有一个URI(可能)指向我刚刚选择的文件夹。 Here's an example output: Folder added: content://com.android.externalstorage.documents/tree/FF30-E80D%3AImages/document/FF30-E80D%3AImages . 这是一个示例输出: Folder added: content://com.android.externalstorage.documents/tree/FF30-E80D%3AImages/document/FF30-E80D%3AImages

Now, I need to get the absolute path, something translating in this case to /storage/sdcard1/Images . 现在,我需要获得绝对路径,在这种情况下转换为/storage/sdcard1/Images I found some code in aFileChooser which seems to work, but not for SD cards, which is my main use case. 我在aFileChooser中找到了一些似乎有效的代码,但不是SD卡,这是我的主要用例。

I could probably split the URI on %3A , if the preceding part is "primary", then use the first external storage path, if it's something else, use the second one, but it feels wrong… There's probably a more deterministic way to achieve this? 我可以在%3A上拆分URI,如果前面的部分是“主要的”,那么使用第一个外部存储路径,如果它是别的,使用第二个,但感觉不对......可能有更确定的方法来实现这个?

Edit: here's some background on this. 编辑:这里有一些背景知识。 I don't need the path to manage the files. 我不需要管理文件的路径。 But how to present this to the user? 但是如何向用户呈现这个? I definitely can't show him content://com.android.externalstorage.documents/tree/FF30-E80D%3AImages/document/FF30-E80D%3AImages , that's just scary. 我绝对无法向他展示content://com.android.externalstorage.documents/tree/FF30-E80D%3AImages/document/FF30-E80D%3AImages ,这简直太吓人了。

Now, I need to get the absolute path, something translating in this case to /storage/sdcard1/Images. 现在,我需要获得绝对路径,在这种情况下转换为/ storage / sdcard1 / Images。

That is not possible. 这是不可能的。 There is no requirement that the Uri point to a file at all, let alone a file that you can access. Uri根本不需要指向文件,更不用说可以访问的文件了。

Use a ContentResolver and openInputStream() to get an InputStream on the content and read it in that way. 使用ContentResolveropenInputStream()在内容上获取InputStream并以这种方式读取它。 This is not significantly different than how you would handle a URL to a Web server (use HttpUrlConnection to get an InputStream on the Web page or other content). 这与处理Web服务器的URL的方式没有显着差异(使用HttpUrlConnection在Web页面或其他内容上获取InputStream )。

But how to present this to the user? 但是如何向用户呈现这个?

I don't know what "this" is. 我不知道“这个”是什么。 However, you can get the suggested display name associated with the content . 但是, 您可以获取与内容关联的建议显示名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM