22

Actually, I need to open the default Download folder from my application. Is it possible? If yes, then please provide some reference.

I am able to get the path of Download folder with the help of:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

Any help will be well appreciated.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
abhishek kumar gupta
  • 2,189
  • 6
  • 35
  • 56

3 Answers3

63

You can show the recent downloads activity with the following Intent

startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));

Available since API 9

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
  • 1
    Hi Ion Aalbers, thanks for the reply. Is there any way(Intent) to pick the file from Download folder? – abhishek kumar gupta Feb 07 '14 at 09:17
  • @abhishekkumargupta What do you want to do with this file? Open it? – Ion Aalbers Feb 07 '14 at 10:56
  • Hi Ion Aalbers, I want to allow the user to pick the desired file from the Download folder, and upload to the server. Currently I am able to do this task with the help of 3rd party file explorer. – abhishek kumar gupta Feb 10 '14 at 08:02
  • Hello guys. Have you tested that on samsung phones? Because I have tested it with samsung phone (running Nugat) and it was opening a Download Histor but with empty content. The section had not any downloaded file. What I have to do to find my recent download file was to go manually to Samsung Files app and find it under Internal Storage/Downloads. Any idea why that's happening and how can I fix that on Samsung devices? – Tzegenos Jan 15 '18 at 08:12
  • @Tzegenos it is happening because "The Downloads app only shows downloads downloaded via DownloadManager, not all files in the directory", https://stackoverflow.com/a/20472865/5222156 – madim Aug 22 '18 at 11:06
  • After writing the file, if i open the downloads folder programmatically using startActivity(Intent(DownloadManager.ACTION_VIEW_DOWNLOADS)). File is not visible. But if i open from file explorer of phone, then the file is visible. Any idea, why this is happening ? – K Pradeep Kumar Reddy Jul 27 '22 at 10:13
  • Help!! Any way to open the specific download folder using this? – DiLDoST Feb 18 '23 at 18:41
12

Samsung solution:

public static void openDownloads(@NonNull Activity activity) {
    if (isSamsung()) {
        Intent intent = activity.getPackageManager()
                .getLaunchIntentForPackage("com.sec.android.app.myfiles");
        intent.setAction("samsung.myfiles.intent.action.LAUNCH_MY_FILES");
        intent.putExtra("samsung.myfiles.intent.extra.START_PATH", 
                getDownloadsFile().getPath());
        activity.startActivity(intent);
    }
    else activity.startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
}

public static boolean isSamsung() {
    String manufacturer = Build.MANUFACTURER;
    if (manufacturer != null) return manufacturer.toLowerCase().equals("samsung");
    return false;
}

public static File getDownloadsFile() {
    return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}

Found by decompiling the Samsung My Files app dex and seeing how the MainActivity deals with intents.

public static final String MYFILES_NOTIFICATION_LAUNCH_INTENT_NAME = "samsung.myfiles.intent.action.LAUNCH_MY_FILES";
public static final String NOTIFICATION_START_PATH = "samsung.myfiles.intent.extra.START_PATH";
public static final String START_PATH = "FOLDERPATH";

if (Constant.MYFILES_NOTIFICATION_LAUNCH_INTENT_NAME.equals(intent.getAction())) {
                String startNotificationPath = intent.getStringExtra(Constant.NOTIFICATION_START_PATH);
                if (startNotificationPath != null) {
                    Bundle newArgument = new Bundle();
                    newArgument.putString(Constant.START_PATH, startNotificationPath);
                    if (new File(startNotificationPath).exists()) {
                        startBrowser(513, newArgument);
                    }
                }
                intent.removeExtra(Constant.NOTIFICATION_START_PATH);
            }
Cayce Williams
  • 398
  • 4
  • 8
0

I had similar problem showing files in Downloads in Samsung and LG and i just added file-parameter "downloaded" for DownloadManager and file showing in Downloads:

  DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        downloadManager.addCompletedDownload(file.getName(), file.getName(), true,
                "application", file.getPath(), file.length(), true);
Teamothy
  • 2,000
  • 3
  • 16
  • 26