从内容URI获取目录路径 [英] Get directory path from content URI
问题描述
我正在使用SDK的 ACTION_OPEN_DOCUMENT_TREE
目的是让用户选择目录.这是代码:
I'm using the SDK's ACTION_OPEN_DOCUMENT_TREE
intent to let the user select a directory. 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());
}
我有一个URI(可能)指向我刚刚选择的文件夹.这是示例输出:添加的文件夹:content://com.android.externalstorage.documents/tree/FF30-E80D%3AImages/document/FF30-E80D%3AImages
.
I have an URI (probably) pointing to the folder I just selected. Here's an example output: Folder added: content://com.android.externalstorage.documents/tree/FF30-E80D%3AImages/document/FF30-E80D%3AImages
.
现在,我需要获取绝对路径,在这种情况下,该内容会转换为/storage/sdcard1/Images
.我在中找到了一些代码aFileChooser 似乎有效,但不适用于SD卡,这是我的主要用例.
Now, I need to get the absolute path, something translating in this case to /storage/sdcard1/Images
. I found some code in aFileChooser which seems to work, but not for SD cards, which is my main use case.
我可能会在%3A
上拆分URI,如果前面的部分是"primary",则使用第一个外部存储路径,如果还有其他原因,请使用第二个外部存储路径,但是错误的…可能有确定性更高的方法来实现这一目标?
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?
这是一些背景知识.我不需要管理文件的路径.但是如何向用户展示呢?我绝对不能告诉他 content://com.android.externalstorage.documents/tree/FF30-E80D%3AImages/document/FF30-E80D%3AImages
,这太可怕了.
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.
推荐答案
现在,我需要获取绝对路径,在这种情况下,该内容会转换为/storage/sdcard1/Images.
Now, I need to get the absolute path, something translating in this case to /storage/sdcard1/Images.
那是不可能的.完全不需要 Uri
指向文件,更不用说您可以访问的文件了.
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.
使用 ContentResolver
和 openInputStream()
获得内容上的 InputStream
并以这种方式读取.这与您处理Web服务器URL的方式没有明显不同(使用 HttpUrlConnection
在网页或其他内容上获取 InputStream
).
Use a ContentResolver
and openInputStream()
to get an InputStream
on the content and read it in that way. 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).
但是如何向用户展示呢?
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.
这篇关于从内容URI获取目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!