Search code examples
androidandroid-intentandroid-external-storage

Opening download directory using Intent


I'm trying to open the download directory in my emulator using Intent.ACTION_GET_CONTENT.

I can successfully get the path of the downloads directory and list all the files in it, but I can't seem to open it with an intent. It only displays the Recent directory.

Successfully logs all the files in that download directory, so the path to it is not incorrect

File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
File[] files = f.listFiles();
if (files != null) {
     for (File inFile : files) {
           Log.d(TAG, "onPermissionGranted: File name: " + inFile.getName());;
     }
}

But when using the intent:

public void openDirectory() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());

        Log.d(TAG, "openDirectory: " + uri);
        //logs this: /storage/emulated/0/Download
        

        intent.setDataAndType(uri, "file/*");
        startActivity(Intent.createChooser(intent, "Select keystore"));
    }

It always displays Recent

enter image description here

Instead of Downloads

enter image description here

I have to manually go to the Downloads tab

enter image description here

How can I make it so that the intent goes to the Downloads without having the need to switch?


Solution

  • This is how you do it:

     Intent intent=new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
     startActivity(intent);