0

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;
        }
    }
}
Eddie K
  • 503
  • 4
  • 18
  • Answered this earlier today. It has to do with AsyncTasks not running in parallel, check the answer and comments on the duplicate link. – Gabe Sechan Jan 03 '15 at 03:43
  • @GabeSechan Thanks, but the difference here is that I only call the AsyncTask once, so executeOnExeucutor has no effect. Should I try calling an AsyncTask for every url instead? – Eddie K Jan 03 '15 at 03:50
  • Yes. If you're only launching one AsyncTask with multiple parameters, then you're downloading one after the other rather than doing multiple at once. Just remember to use executeOnExecutor, so they run in parallel. – Gabe Sechan Jan 03 '15 at 03:52

0 Answers0