我有大约100个自定义对象,每个对象需要从网上下载的两个图像(徽标和圆形/圆形徽标)供离线使用。我目前正在使用AsyncTask。我有两个url的ArrayLists,每个类型的图像一个,我作为参数发送。整个过程大约需要一分钟,用户需要等到它完成,因为应用程序围绕下载的项目。有什么方法可以加快速度吗?当我在iOS中实现它时,在同一网络上大约需要10秒钟。
new LogoDownloadTask(resources).execute(logoURLs, roundLogoURLs);
// ...
private class LogoDownloadTask extends AsyncTask<ArrayList, Integer, Void> {
private ArrayList<Organization> resources;
public LogoDownloadTask(ArrayList<Organization> resources) {
this.resources = resources;
}
@Override
protected Void doInBackground(ArrayList... urls) {
try {
// Download logos
for (int i = 0; i < urls[0].size(); i++) {
URL url = (URL)urls[0].get(i);
Bitmap bm = downloadImage(url);
Organization org = resources.get(i);
if (bm == null) {
Log.v(TAG, "ERROR DOWNLOADING LOGO FOR " + org.getName());
} else {
org.setLogo(bm);
}
}
// Download round logos
for (int i = 0; i < urls[1].size(); i++) {
URL url = (URL)urls[0].get(i);
Bitmap bm = downloadImage(url);
Organization org = resources.get(i);
if (bm == null) {
Log.v(TAG, "ERROR DOWNLOADING ROUND LOGO FOR " + org.getName());
} else {
org.setRoundLogo(bm);
}
}
} catch (Exception e) {
Log.v(TAG, "FAILURE REACHING SERVER");
}
return null;
}
@Override
protected void onPostExecute(Void v) { /* ... */ }
private Bitmap downloadImage(URL url) {
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
return bitmap;
} catch (IOException e) {
return null;
}
}
}