英文:
From Uri to Environment.DIRECTORY_DCIM
问题
以下是翻译好的代码部分:
public void startChoose(View view) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(Intent.createChooser(i, "选择目录"), 8010);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 8010:
mypath = data.getData().toString();
break;
}
}
他选择的目录会被存储在 mypath
变量中,例如选择了 DCIM 目录:
content://com.android.externalstorage.documents/tree/primary%3ADCIM
现在,是否可能从 content://com.android.externalstorage.documents/tree/primary%3ADCIM
转换为 Environment.DIRECTORY_DCIM
,以及如何实现?
换句话说,当用户选择了 DCIM 目录,我想要获取变量 Environment.DIRECTORY_DCIM
对应于 mypath
,有什么方法可以实现这一点?
英文:
A user chooses a directory through the following code:
public void startChoose(View view) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(Intent.createChooser(i, "Choose directory"), 8010);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 8010:
mypath = data.getData().toString();
break;
}
}
His directory is stored in mypath
as following (e.g. with DCIM):
content://com.android.externalstorage.documents/tree/primary%3ADCIM
Now, is it possible to go from content://com.android.externalstorage.documents/tree/primary%3ADCIM
to Environment.DIRECTORY_DCIM
and how?
Asked differently, I would like to get the variable Environment.DIRECTORY_DCIM
for mypath
when the User chooses the DCIM directory. How can one do that?
答案1
得分: 1
不。您获得的Uri
不必对应一个文件(例如,它指向Google Drive中的文档)。即使它对应一个文件,它也不必位于与Environment
相关联的目录中(例如,它指向一个自定义目录中的文件)。
请只使用Uri
。
英文:
> Now, is it possible to go from content://com.android.externalstorage.documents/tree/primary%3ADCIM to Environment.DIRECTORY_DCIMand how?
No. The Uri
that you get back does not have to correspond to a file (e.g., it points to a document in Google Drive). Even if it corresponds to a file, it does not have to reside in a directory that is tied to Environment
(e.g., it points to a file in a custom directory).
Please just use the Uri
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论