Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

android - Downloading multiple files one by one using AsyncTask?

I'm trying to download multiple files one by one (file is downloaded, we start downloading the next file). Using this method I can keep track of the files being downloaded. The problem is that I'm using the following code for executing a Task:

                File file = null;                
                for(int i=0; i< videos.length; i++) {
                     file = new File(root.getPath() + videos[i]);
                     boolean exists = file.exists();
                     if(exists){
                         //tv.append("

"+fileNames[i]+" already exists");
                         continue;
                     }
                     else {
                         currentFile = videos[i];
                         new DownloadFileAsync().execute(videoURL+videos[i],videos[i]);            

                     }
                     file = null;
                }

As you can see, I call new DownloadFileAsync().execute(videoURL+videos[i],videos[i]); in a loop which obviously start a task for each of the files and downloads them simultaneously.

My question is: How can I run execute the task for a specific file, check if it has been downloaded- if yes, proceed with next file by executing a task for it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand correctly, you do not want to download all the files at the same tim e(simultaneously) but one by one (serially). In order to do so build a String array with the URLs to download, and call execute() with that array.

Example: Assuming that your DownloadFileAsync expects String as a parameter to it's doInBackground method, you would call to:

new DownloadFileAsync().execute(url1, url2, url3, url4, video1, video2, video3, video4);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...