从Uri到Environment.DIRECTORY_DCIM

huangapple go评论83阅读模式
英文:

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_DCIMand 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.

huangapple
  • 本文由 发表于 2020年10月18日 05:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64407682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定