user2958359
user2958359

Reputation: 131

Downloading Image given url faster in android

I have some url of images, I want to download those and set to my imageView in android. I am using the following code.It is just doing the task. But the process seems to me too slow. I know that it depends on my internet connection speed in my device, but still is there any faster way to speed up loading the image?

import java.io.InputStream;
import java.lang.ref.WeakReference;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;

        public ImageDownloaderTask(ImageView imageView) {
                imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        // Actual download method, run in the task thread
        protected Bitmap doInBackground(String... params) {
                // params comes from the execute() call: params[0] is the url.
                return downloadBitmap(params[0]);
        }

        @Override
        // Once the image is downloaded, associates it to the imageView
        protected void onPostExecute(Bitmap bitmap) {
                if (isCancelled()) {
                        bitmap = null;
                }

                if (imageViewReference != null) {
                        ImageView imageView = imageViewReference.get();
                        if (imageView != null) {

                                if (bitmap != null) {
                                        imageView.setImageBitmap(bitmap);
                                } else {
                                        imageView.setImageDrawable(imageView.getContext().getResources()
                                                        .getDrawable(R.drawable.imgloading));
                                }
                        }

                }
        }

        static Bitmap downloadBitmap(String url) {
                final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
                final HttpGet getRequest = new HttpGet(url);
                try {
                        HttpResponse response = client.execute(getRequest);
                        final int statusCode = response.getStatusLine().getStatusCode();
                        if (statusCode != HttpStatus.SC_OK) {
                                Log.w("ImageDownloader", "Error " + statusCode
                                                + " while retrieving bitmap from " + url);
                                return null;
                        }

                        final HttpEntity entity = response.getEntity();
                        if (entity != null) {
                                InputStream inputStream = null;
                                try {
                                        inputStream = entity.getContent();
                                        final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                        return bitmap;
                                } finally {
                                        if (inputStream != null) {
                                                inputStream.close();
                                        }
                                        entity.consumeContent();
                                }
                        }
                } catch (Exception e) {
                        // Could provide a more explicit error message for IOException or
                        // IllegalStateException
                        getRequest.abort();
                        Log.w("ImageDownloader", "Error while retrieving bitmap from " + url);
                } finally {
                        if (client != null) {
                                client.close();
                        }
                }
                return null;
        }

}

Waiting for any hint or source code that will just do the same task but faster. Thanx

Upvotes: 2

Views: 4250

Answers (2)

kodartcha
kodartcha

Reputation: 1073

Maybe it is not the best and cleanest way to do this but in my opinion and from my experience it is the fastes.

I like to use WebView to load images and works great when you have a lot of them and it is simple as that:

image.setBackgroundColor(0);
image.loadDataWithBaseURL("", "<img src='YOUR URL HERE'/>", 
                          "text/html", "UTF-8", "");

Where image is a WebView:

WebView image = (WebView) findViewById(R.id.imagewebview);

Upvotes: 1

Basic Coder
Basic Coder

Reputation: 11422

You could use caching. I am using Square's Picasso for loading images from the web and displaying them in an ImageView. Picasso has several Caching implementations and is pretty straight forward to use.

https://github.com/square/picasso

Upvotes: 2

Related Questions