AhmadReza Molaei
AhmadReza Molaei

Reputation: 489

Download multiple concurrent files

I need download multiple files in one time (About 100 files)

It does not matter whether the download is synchronized

And the important thing is that all files be downloaded.

My code for getting urls and file names:

for (int i = 0; i < AssetData.size(); i++){
        String item = AssetData.get(i).toString();
        String name[] = item.split("/");
        String Url = setting.Main_Domain+"/"+item;// Url for downloading
        String fname =name[name.length-1] ;// File name like: test.txt
        File file2 =  new File(getFilesDir(),item.replace(fname,"")); // Parent File like: data/user/0/com.test.app/data/
        if(!file2.exists()){file2.mkdir();}

    }

The size of the files is small and all together is about 3 megabytes.

Upvotes: 2

Views: 1728

Answers (2)

ucMax
ucMax

Reputation: 5428

This seems to me ion is a pretty elegant solution for you. It's easy to use and has many futures like connection pooling and reuse via HTTP Connection...

Upvotes: 0

Khemraj Sharma
Khemraj Sharma

Reputation: 58934

You can implement your code with using this library. You can download multiple files concurrent or you can start next download after one is completed.

https://github.com/MindorksOpenSource/PRDownloader

This is how your code will look

int downloadId = PRDownloader.download(url, dirPath, fileName)
                        .build()
                        .setOnStartOrResumeListener(new OnStartOrResumeListener() {
                            @Override
                            public void onStartOrResume() {

                            }
                        })
                        .setOnPauseListener(new OnPauseListener() {
                            @Override
                            public void onPause() {

                            }
                        })
                        .setOnCancelListener(new OnCancelListener() {
                            @Override
                            public void onCancel() {

                            }
                        })
                        .setOnProgressListener(new OnProgressListener() {
                            @Override
                            public void onProgress(Progress progress) {

                            }
                        })
                        .start(new OnDownloadListener() {
                            @Override
                            public void onDownloadComplete() {

                            }

                            @Override
                            public void onError(Error error) {

                            }
                        });

Isn't this simple. :)

Upvotes: 1

Related Questions