1

I have a GridView with many ImageView items. For each of the item I lazy load a thumbnail imahge from WebService using HttpClient. I crate a new HttpClient for every image i download. The thumbnails are 2-4kB small. I noticed the download is slow, images are loaded 1 by 1 and each of them is downloaded in 1s. Is it possible to speed up the process?

public Bitmap downloadPhoto( String url ) {

    try {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpClient client = new DefaultHttpClient(params);
        HttpUriRequest request = new HttpGet(url);
        if ( this.authToken != null ) {
            request.setHeader(AUTH_TOKEN_NAME, authToken);
        }
        request.setHeader(USER_AGENT_PROPERTY, AGENT_NAME);

        HttpResponse response = client.execute(request);
        if ( response.getStatusLine().getStatusCode() == HttpStatus.SC_OK ) {
            // read the content
            long contentLenght = response.getEntity().getContentLength();
            BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(response.getEntity());

            Bitmap image = BitmapFactory.decodeStream(bufferedHttpEntity.getContent());
            Log.e(TAG, "Bitmap != null " + (image != null) );
            return image;
        } else {
            Log.e(TAG, "HTTP ERROR while executing method: downloadImage: " + response.getStatusLine().getStatusCode());
        }

    } catch (Exception e) {
        Log.e(TAG, "Exception while executing method: downloadImage: " + e.getMessage());
        return null;
    }
    return null;
}
cubesoft
  • 3,448
  • 7
  • 49
  • 91

3 Answers3

1

I would recommend using the AndroidHttpClient which has some good default settings, including the ThreadSafeClientConnManager and thus can be shared between threads. You can create a custom AsyncTask and pass the client in the c'tor.

You shouldn't need to recreate the client each time then which also takes time - just listen in for lifecycle events and shutdown/recreate as necessary.

Whats wrong with multiple connections being established? Is this a design requirement?

0

Make the downloading part of custom class that extends AsyncTask. This will make the make sure that images are downloaded as a background thread.

Refer: http://developer.android.com/reference/android/os/AsyncTask.html

phanij
  • 46
  • 2
  • This will result in multiple connections being established to single IP. This violates the "Two connection limit rule". – cubesoft Mar 16 '12 at 10:31
  • ThreadSafeHttpClientConnManager is the best bet here. The following link answers more questions about it http://stackoverflow.com/questions/1281219/best-practice-to-use-httpclient-in-multithreaded-environment – phanij Mar 20 '12 at 02:46
0

Are you using a single threads to download images? You might try using ThreadPoolExecutor to use a fixed pool of threads for simultaneous download.

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104