尝试使用AsyncTask的做下载一些图片文件 [英] Trying to use AsyncTask do download some image files

查看:171
本文介绍了尝试使用AsyncTask的做下载一些图片文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有内部存储数组列表的文件,我需要在后台线程下载的列表。我最初的想法是AsyncTask的应该是弥补了这一任务。但是,我有一个问题,我不知道如何提供我的列表doInBackground方法。

我的ArrayList被定义为

 私人的ArrayList<字符串> FilesToDownload =新的ArrayList<字符串>();
 

我DownloadFiles子(现在写的方式)应该被调用: 新DownloadFiles()执行(URL1,URL2,URL3,等。);

这是不适合我,因为我从来不知道有多少网址我都会有。这是pretty的很多改变从情况而定。所以,我想以某种方式为我的ArrayList传递给doInBackground方法。

我想转换为数组的toArray():

 新DownloadFiles()执行(FilesToDownload.toArray())。
 

不过,日食告诉我,执行不适用于参数对象[]。 建议是,转换为URL [],但是当我尝试,我得到了非法铸造错误和应用程序崩溃。

看来doInBackground必须与在可变参数类型参数(URL ...的URL)来实现。

任何想法如何解决我的问题?谢谢你。

 类DownloadFiles扩展的AsyncTask< URL,为Integer,Long> {
     保护龙doInBackground(网址...网址){
         诠释计数= urls.length;
         长totalSize = 0;
         的for(int i = 0; I<计数;我++){
             Log.d(Evento,与&&安培;&安培;&安培;下载:+ FilesToDownload.get(ⅰ)的ToString());
// totalSize + = Downloader.downloadFile(网址[I]);
// publishProgress((INT)((I /(浮点)数)* 100));
         }
         返回totalSize;
     }
     保护无效onProgressUpdate(整数...进度){
        // setProgressPercent(进展[0]);
     }
     保护无效onPostExecute(长效){
       //的ShowDialog(已下载+结果+字节);
     }
}
 

解决方案

的AsyncTask 与URL的参数类型的声明,但你想通过字符串(或的ArrayList )的对象。无论是prepare的 URL对象的数组在调用程序或修改 DownloadFiles 接受字符串,而不是URL参数,然后将其转换字符串执行()方法。

更重要的是,因为你正在访问 FilesToDownload 从内部执行(),你并不需要通过什么执行(),你可以申报 DownloadFiles 的第一个泛型参数为虚空

I have list of files stored inside arraylist that I need to download in background thread. My initial thought is that AsyncTask should be up for that task. But, I have a problem, I don't know how to supply my list to doInBackground method.

My arraylist is defined as

private ArrayList<String> FilesToDownload = new ArrayList<String>();

My DownloadFiles subclass (the way it is written now) should be called with: new DownloadFiles().execute(url1, url2, url3, etc.);

This is not suitable for me since I never know how many urls I will have. It is pretty much changing from case to case. So, I would like somehow to pass my arraylist to doInBackground method.

I tried to convert to array with toArray():

new DownloadFiles().execute(FilesToDownload.toArray());

But, eclipse tells me that execute is not applicable for argument Object[]. Suggestion was to cast to URL[], but when I tried that I got illegal casting error and app crashed.

It seems that doInBackground has to be implemented with parameters in varargs type (URL... urls).

Any ideas how to solve my problem? Thanks.

class DownloadFiles extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;        
         for (int i = 0; i < count; i++) {
             Log.d("Evento", "&&&& downloading: " + FilesToDownload.get(i).toString());
//             totalSize += Downloader.downloadFile(urls[i]);
//             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }
     protected void onProgressUpdate(Integer... progress) {
        //setProgressPercent(progress[0]);
     }
     protected void onPostExecute(Long result) {
       //showDialog("Downloaded " + result + " bytes");
     }
}

解决方案

Your AsyncTask is declared with a param type of URL, but you're trying to pass String (or ArrayList) objects. Either prepare an Array of URL objects in the calling program or modify DownloadFiles to accept String instead of URL parameters and convert each String to a URL in the execute() method.

Better yet, since you are accessing FilesToDownload from within execute(), you don't need to pass anything to execute() and you could declare the first generic parameter of DownloadFiles as Void.

这篇关于尝试使用AsyncTask的做下载一些图片文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆