I think this is a recurring topic. Here is something I posted on it previously. I was looking for a recommended or consensus approach. Romain mentioned that his technique takes into consideration things like whether the list is scrolling or not.
Here are 4 references that I have found for lazy-loading images into a listview in Android. The idea is to display a placeholder image, get the actual image in the background, update the ImageView in the list when the image is available. I've tried to do this in the simplest way possible using an AsyncTask in an Adapter. The outline of that approach is below. Is it flawed? Is there an agreed approach to handling this common task? Thanks, Carmen http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2/ Tom van Zummeren http://evancharlton.com/thoughts/lazy-loading-images-in-a-listview/ Evan Charlton http://code.google.com/p/shelves/ from Romain Guy http://github.com/commonsguy/cwac-thumbnail from Mark Murphy Approach using AsyncTask within an Adapter Somewhere in the Adapter. Add info about the URL for the image to fetch to the ImageView. Execute an AsyncTask to get the image in the background and load the ImageView. AsyncTask uses the tag to fetch the image. It uses the tag to find the correct view within the ListView. ImageView imageViewByTag = (ImageView) myListView.findViewWithTag( tag); ImageView is updated. Somewhere in the Adapter: myImageView.setTag("http://someimage.jpg"); // try { new GetImage().execute(myImageView); } catch(RejectedExecutionException e) { } // Asynctask private class GetImageInBackground extends AsyncTask<ImageView , ImageView , Long > { Drawable drawable; String tag; @Override protected void onProgressUpdate (ImageView... params){ if (drawable!=null){ ImageView imageViewByTag = (ImageView) myListView.findViewWithTag(tag); // find the ImageView if (imageViewByTag != null) { imageViewByTag.setImageDrawable(drawable); //update it } } } @Override protected Long doInBackground(ImageView... params) { tag = (String) params[0].getTag(); // get the tag from the ImageView if (tag==null){ drawable = getPlaceholder(); // get a default drawable }else{ bm =getDrawablefromURL(String (tag); // a method to get drawable from a URL } publishProgress(params[0]); return null; } } On Sat, Apr 10, 2010 at 3:10 PM, Prajakta Shitole <[email protected]>wrote: > I was able to resolve the issue, just made simple tweaks like removing the > drawableMap we just need the imageCache. > > > On Sat, Apr 10, 2010 at 10:51 AM, Prajakta Shitole > <[email protected]>wrote: > >> Forgot to paste my code:- >> >> class IconicAdapter extends ArrayAdapter { >> Activity context; >> Context context2; >> HashMap<String,String> ret = new HashMap<String,String>(); >> AsyncLoader async;// = new AsyncLoader(); >> private ListView listView; >> HashMap<String,Bitmap> bit = new HashMap<String,Bitmap>(); >> >> >> IconicAdapter(Activity context) { >> super(context, R.layout.exclusive, items); >> this.context=context; >> async = new AsyncLoader(); >> } >> >> public View getView(final int position, View convertView, >> ViewGroup parent) { >> View row_inflated = View.inflate(context, R.layout.exclusive,null); >> wrapper = new ViewWrapper(row_inflated); >> row_inflated.setTag(wrapper); >> >> wrapper.getSongName().setText(items[position]); >> wrapper.getSingerName().setText(singer_name[position]); >> final ImageView imgview = wrapper.getIcon(); >> imgview.setTag(myRemoteImages[position]); >> Bitmap cachedImage = async.loadDrawable(myRemoteImages[position], new >> ImageCallback() { >> public void imageLoaded(Bitmap imageBitmap, String imageUrl) { >> imgview.setImageBitmap(imageBitmap); >> notifyDataSetChanged(); >> } >> }); >> if(cachedImage!=null) >> imgview.setImageBitmap(cachedImage); >> return(row_inflated); >> } >> } >> >> >> ====================================================================== >> AsynLoader is as below:- >> >> public class AsyncLoader{ >> HashMap<String,Bitmap> bit = new HashMap<String,Bitmap>(); >> private HashMap<String, SoftReference<Bitmap>> imageCache; >> private HashMap<String, SoftReference<Bitmap>> drawableMap; >> public AsyncLoader() { >> drawableMap = new HashMap<String, SoftReference<Bitmap>>(); >> imageCache = new HashMap<String, SoftReference<Bitmap>>(); >> } >> public Bitmap loadDrawable(final String imageUrl, final ImageCallback >> imageCallback) { >> if (drawableMap.containsKey(imageUrl)) { >> SoftReference<Bitmap> softReference = >> imageCache.get(imageUrl); >> Bitmap drawable = softReference.get(); >> if (drawable != null) { >> return drawable; >> } >> } >> final Handler handler = new Handler() { >> @Override >> public void handleMessage(Message message) { >> imageCallback.imageLoaded((Bitmap) message.obj, imageUrl); >> } >> }; >> new Thread() { >> @Override >> public void run() { >> Bitmap bitMap = loadImageFromUrl(imageUrl); >> imageCache.put(imageUrl, new >> SoftReference<Bitmap>(bitMap)); >> Message message = handler.obtainMessage(0, bitMap); >> handler.sendMessage(message); >> } >> }.start(); >> return null; >> } >> >> public static Bitmap loadImageFromUrl(String url){ >> >> >> URL aURL; >> Bitmap bitmap=null;//=this.bm; >> try { >> aURL = new URL(url); >> URLConnection conn; >> conn = aURL.openConnection(); >> conn.connect(); >> InputStream is= conn.getInputStream(); >> BufferedInputStream bis = new BufferedInputStream(is); >> bitmap = BitmapFactory.decodeStream(bis); >> // bitMap.put(p,bm); >> bis.close(); >> is.close(); >> >> >> } catch (MalformedURLException e) { >> e.printStackTrace(); >> } catch (IOException e) { >> e.printStackTrace(); >> } >> return bitmap; >> } >> >> public interface ImageCallback { >> public void imageLoaded(Bitmap imageBitmap, String imageUrl); >> } >> } >> >> >> Thanks, >> Prajakta >> >> >> >> On Sat, Apr 10, 2010 at 10:21 AM, praj <[email protected]> wrote: >> >>> Hi, >>> >>> I was trying to implement the lazy loading of images in the list view. >>> For implementing this i was referring to post by Tom van Zummeren. >>> (http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android- >>> part-2/<http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2/>), >>> but my images keep on changing in the list. Please can >>> anyone let me know if they were able to get this working properly >>> without having the images drawn again and again or let me know if I >>> have missed on anything or doing something wrong. >>> >>> Thanks, >>> Prajakta >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Android Developers" group. >>> To post to this group, send email to [email protected] >>> To unsubscribe from this group, send email to >>> [email protected]<android-developers%[email protected]> >>> For more options, visit this group at >>> http://groups.google.com/group/android-developers?hl=en >>> >>> To unsubscribe, reply using "remove me" as the subject. >>> >> >> > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected]<android-developers%[email protected]> > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Carmen http://www.twitter.com/CarmenDelessio http://www.talkingandroid.com http://www.facebook.com/BFFPhoto http://www.twitter.com/DroidDrop -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

