1

I am getting image url path from a service,which i have to display in my list view(which also has to display other text which i get from service).Here is the code for image download from url path(which i got from http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html) and my get view() in custom adapter:

public class ImageDownloader {

public void download(String url, ImageView imageView) {
     if (cancelPotentialDownload(url, imageView)) {
         BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
         DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
         imageView.setImageDrawable(downloadedDrawable);
         task.execute(url);
     }
}

private static boolean cancelPotentialDownload(String url, ImageView imageView) {
    BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);

    if (bitmapDownloaderTask != null) {
        String bitmapUrl = bitmapDownloaderTask.url;
        if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
            bitmapDownloaderTask.cancel(true);
        } else {
            // The same URL is already being downloaded.
            return false;
        }
    }
    return true;
}

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

        public BitmapDownloaderTask(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();
                BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
                // Change bitmap only if this process is still associated with it
                if (this == bitmapDownloaderTask) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }

    private static BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
        if (imageView != null) {
            Drawable drawable = imageView.getDrawable();
            if (drawable instanceof DownloadedDrawable) {
                DownloadedDrawable downloadedDrawable = (DownloadedDrawable)drawable;
                return downloadedDrawable.getBitmapDownloaderTask();
            }
        }
        return null;
    }

    static class DownloadedDrawable extends ColorDrawable {
        private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;

        public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
            super(Color.WHITE);
            bitmapDownloaderTaskReference =
                new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
        }

        public BitmapDownloaderTask getBitmapDownloaderTask() {
            return bitmapDownloaderTaskReference.get();
        }
    }

    public static Bitmap downloadBitmap(String src) {
         try {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

            StrictMode.setThreadPolicy(policy); 
            // Log.e("src",src);
             URL url = new URL(src);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setDoInput(true);
             connection.connect();
             InputStream input = connection.getInputStream();
             Bitmap myBitmap = BitmapFactory.decodeStream(input);
            // Log.e("Bitmap","returned");
             return myBitmap;
         } catch (IOException e) {
             e.printStackTrace();
             //Log.e("Exception",e.getMessage());
             return null;
         }

}

    @Override
    public View getView(final int position, View view, ViewGroup arg2) {
        // TODO Auto-generated method stub

        if (view == null) {

                view = inflater.inflate(R.layout.hospital_row,                                     null);

                holder = new MyViewHolder();

                holder.hospName = (TextView) view
                        .findViewById(R.id.hospname);

                holder.hospImage = (ImageView) view
                        .findViewById(R.id.hospitalicon);


                view.setTag(holder);
            }
            holder = (MyViewHolder) view.getTag();

            values = valueList.get(position);

            // for image
            path = values.getImagePath();
            ImageDownloader idm=new ImageDownloader();
            idm.download(path, holder.hospImage);


            holder.hospName.setText(values.getHospitalName());
            }

But the problem i am facing is each time i scroll, get view() is getting called and the whole list of images are getting reloaded from async task.So is there a way to not reload or refresh the whole list when i scroll... Note :I dont want to use an LRUCache (or any other memory cache)to cache them as there are many images(around 1000) which can consume heavy memory.

user2106108
  • 163
  • 11
  • There are bunch of image loader libraries that support loading the image from disk cache in much more simple ways like this one: http://square.github.io/picasso/ or Universal-image-loader library. Their usage is straight forward in only few (or even one line of code). On the other hand, why you are recreating the ImageDownloader object again whenever getView() is called? Is the object creation overhead burdening the listview? – Nikola Despotoski Jul 08 '13 at 02:09
  • Ya i have removed it and made it has single instance...but my problem is the async task inside the imagedownload class is getting called everytime i scroll which cause the image view to blink...i have to stop that without using any cache...so any solutions?????? – user2106108 Jul 08 '13 at 09:13

1 Answers1

0

Please remove this code from getView.

ImageDownloader idm=new ImageDownloader(); idm.download(path, holder.hospImage);

And all image store in ArrayList and arraylist data set in getView.

nilesh patel
  • 834
  • 1
  • 5
  • 10
  • So u are telling me to download all the images from url path into a arraylist and set it with corresponding imageview in listview..But if there are around 1000 images wont it consume a lot of time to set images in listview?? – user2106108 Jul 08 '13 at 09:50
  • use lureCache for set the images – nilesh patel Jul 08 '13 at 09:53