Android 下载管理器多链接下载

huangapple go评论121阅读模式
英文:

Android Download Manager multiple downloading for links

问题

我创建了一个录音应用程序,用户可以将数据上传到Google Firebase,也可以恢复数据。这就是我的应用程序所做的一切。现在,我测试了恢复过程,使用了600个文件(mp3文件),然后我发现当我调用下载管理器时,文件开始下载,但它们一个接一个地下载,这使得过程非常耗时。所以,有没有办法让下载管理器同时下载所有文件。

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);
}

因此,我通过将链接、路径和文件名发送到上面的方法中来下载文件,然后任务会添加到Android设备的下载管理器中。如何允许下载管理器同时下载它们,就像在其他浏览器中一样呢?

英文:

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.

答案1

得分: 2

如果您正在使用下载管理器进行下载,那么我们一次只能下载一个文件,这实际上取决于设备。许多手机都配备了先进的下载管理器,但有些手机只有简单的管理器,一次只能下载一个文件。

解决方案在于,我们可以使用以下代码来下载文件,它非常好用,而且总是正常工作。它可以同时下载所有文件。

以下是代码:

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()) { // 决定要执行什么操作

    } 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();
    }
}

以上代码将会在手机存储中下载文件,希望对他人有所帮助。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月10日 20:53:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63340627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定