Search code examples
javaandroidurienvironment

From Uri to Environment.DIRECTORY_DCIM


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?


Solution

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