I have about 100 custom objects, each requiring two images (a logo and a circular/round logo) downloaded from the web for offline use. I'm currently using AsyncTask. I have two ArrayLists of urls, one for each type of image, that I send in as parameters. This entire process takes about one minute, and the user needs to wait until it's finished because the app revolves around the downloaded items. Is there any way to speed this up? It takes about about 10 seconds on the same network when I implement this in iOS.
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;
}
}
}