Search code examples
androidandroid-external-storageandroid-8.1-oreo

Opening Files/DocumentsUI via Intent given the Path


My code:

Uri selectedUri = Uri.parse("file:///sdcard/Music");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "*/*");
intent.setType("*/*");
intent.setClassName("com.android.documentsui", "com.android.documentsui.files.FilesActivity");
startActivity(intent);

But its always opens only the Downloads tab, not Music on my sdcard.


Solution

  • Action:
    android.intent.action.VIEW
    
    Data:
    content://com.android.externalstorage.documents/document/primary%3ADownload%2FMyDir
    
    Mime:
    vnd.android.document/directory
    
    Flags:
    These don't seem to be critical
    

    This intent will invoke DocumentsUI (and likely other file managers) and if you pick DocumentsUI it will open to the provided path. %3A for the first / and %2F for subsequent / as you nest into the file tree.

    Cheers!