Search code examples
javaandroidandroid-download-manager

Android Download Manager multiple downloading for links


I created a recording application, user can upload data to google firebase and also can restore the data. That's it, that's all my app does. Now I tested my restoring process with 600 files (mp3 files), then I saw that when I call download manager then the files start downloading but they download one at a time which makes the process very time-consuming. So is there any way to make Download manager to download all the files simultaneously.

void startDownloading(String DownloadUrl, String DownloadPath, String DownloadName) {

                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadUrl));
                request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                request.setTitle("Data");
                request.setDescription("Journals");
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
                request.setDestinationInExternalPublicDir(DownloadPath, DownloadName);
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            
        }

So I download the files by sending the link, path, and name of the file into the above method, and then task get added to DownloadManager in android device. How to allow download Manager to download all of them simultaneously as we do in other browsers.


Solution

  • See if you are downloading using download manager then we can download a single file at a time and it depends on the device actually. Many phones have advance download manager but some have a simple manager who can download a single file at a time.

    So solution is here instead of calling the download manager we can use this code to download files, it is awesome and always works fine. It downloads all the files simultaneously.

    Here is the code:

    
    void startDownloading
    (final String DownloadUrl, String DownloadPath, String DownloadName) {
    
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
    
                final File file = new File(StringManager.phoneStorageLocation + "/"
                        + DownloadPath + "/" + DownloadName);
    
                if (file.exists()) { // decide what to do
    
                } else {
    
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                URL url = new URL(DownloadUrl);
                                ReadableByteChannel rbc = Channels.newChannel(url.openStream());
                                FileOutputStream fos = new FileOutputStream(file);
                                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                                fos.close();
                                rbc.close();
    
                            } catch (IOException ignored) {
                            }
                        }
                    }).start();
                }
            }
    

    The above code will download the file in phone storage, Hope it helps someone.