class DownloadThread extends AsyncTask<URL,Integer,List<Bitmap>>{
protected List<Bitmap> doInBackground(URL... urls){
InputStream in=null;
try{
List<Bitmap> out=new ArrayList<Bitmap>();
for(int i=0;i<urls.length;i++){
URL url=urls[i];
url = new URL("http://mysite/myimage.png");
in=url.openStream();
Bitmap b=BitmapFactory.decodeStream(in);
out.add(b);
publishProgress(i);
}
return out;
}catch(IOException e){
Log.w("networking","Downloading image failed");
return null;
}
finally{
try {
if(in!=null)in.close();
} catch (IOException e) {
Log.w("networking","Closing stream failed");
}
}
}
}
下载网络图片
异步下载图片示例
本文展示了一个使用 AsyncTask 在 Android 应用中异步下载并处理多个图片的示例。通过继承 AsyncTask 类并覆盖 doInBackground 方法,可以有效地从网络加载图片资源。

被折叠的 条评论
为什么被折叠?



