下载多个文件一个接一个使用AsyncTask的?多个、接一个、文件、AsyncTask

2023-09-12 09:22:09 作者:徒手摘星

我想一个一个下载多个文件(文件下载,我们开始下载下一个文件)。使用这种方法,我可以跟踪文件的下载。问题是,我使用的是下面的$ C $下执行任务:

 文件fil​​e = NULL;
                的for(int i = 0; I< videos.length;我++){
                     文件=新的文件(root.getPath()+视频[I]);
                     布尔存在= file.exists();
                     如果(存在){
                         //tv.append("\n\n"+fileNames[i]+已经存在);
                         继续;
                     }
                     其他 {
                         currentFile =视频[I]
                         新DownloadFileAsync()执行(videoURL +视频[I],视频[I])。

                     }
                     文件= NULL;
                }
 

正如你所看到的,我称之为新DownloadFileAsync()执行(videoURL +视频[I],视频[I])。在循环中这显然开始对每个文件的任务,并同时下载它们。

我的问题是,:如何运行任务执行特定文件,检查它是否已经downloaded-如果是的话,通过执行为它的任务继续进行下一个文件?

解决方案 VB 采用AsyncRead下载文件示例

如果我理解正确的话,你不想一(连续)来下载同恬E(同时)中的所有文件,但之一。 为了做到这一点建立与URL的字符串数组下载,并调用执行()与该阵列。

  

例:假设你的 DownloadFileAsync 预计字符串作为   参数,它的 doInBackground方法,你会打电话来:

 新DownloadFileAsync()执行(URL1,URL2,URL3,url4,VIDEO1,VIDEO2,VIDEO3,VIDEO4)。
 

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("\n\n"+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?

解决方案

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