0

I have an app in which i am downloading and displaying images in a thread. Everything is working fine. But when the images are getting loaded same image is getting loaded in two to three places and when we scroll the screen images are loaded correctly. Please can any one of you can give any suggestions to this?

code:

try{
    holder.progress.setVisibility(View.VISIBLE);
    DownLoadImageInAThreadHandler(Item, holder);

}
catch(Exception e)
{
    System.out.println("Exception in Downloading image : " + e.getMessage());
}
return convertView;
}


//This code is outside the getview method
public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
{
    nImageDownLoads++;

    System.out.println("The images being downloaded :" + nImageDownLoads);

    final Handler handler = new Handler()
    {
        @Override  public void handleMessage(Message message)
        {
            holder.imgitem.setImageDrawable((Drawable) message.obj);
            holder.imgitem.setVisibility(View.VISIBLE);
            holder.progress.setVisibility(View.GONE);
        }
    };
    //Thread for downloading the images
    Thread t = new Thread()
    {
        public void run()
        {
            try
            {
                Item.bImageDownLoaded = 2;
                System.out.println("Downloading image      :"+Item.ImageUrl);
                drawable=getDrawableFromUrl(Item.ImageUrl);
                nImageDownLoads--;
                System.out.println("Downloaded image :" + Item.ImageUrl);
                System.out.println("Remaining images for downloading: " + nImageDownLoads);
                if(drawable != null)
                {                                                          
                    Item.bImageDownLoaded = 1;
                    //Send the message to the handler
                    Message message = handler.obtainMessage(1, drawable);
                    handler.sendMessage(message);
                }
                else{
                    int idNoImage = R.drawable.giftsuggestionsnoimage;
                    Drawable dwgNoImg = getParent().getResources().getDrawable(idNoImage);

                    //Send the message to the handler
                    Message message = handler.obtainMessage(1, dwgNoImg);
                    handler.sendMessage(message);
                }
            }
            catch(Exception exp)
            {
                System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
            }
        }

        private Drawable getDrawableFromUrl(String imageUrl) throws IOException
        {
            try
            {
                image = DownloadDrawable(imageUrl, "src");
                //bmp = readBitmapFromNetwork(new URL(imageUrl));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return image;
            //return bmp;
        }
    };
    t.start();
}

Drawable DownloadDrawable(String url, String src_name) throws java.io.IOException
{
    return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()),  src_name);
}
Selvin
  • 6,598
  • 3
  • 37
  • 43
sravanthi V
  • 136
  • 11
  • off topic: before you post code here could you make it "pretty" for example use http://www.prettyprinter.de it was unreadable (or in eclipse use SHIFT+CTRL+F) – Selvin Apr 13 '12 at 11:52
  • Here is a library that will handle all of this for you https://github.com/DHuckaby/Prime – HandlerExploit Apr 13 '12 at 12:27

2 Answers2

0

Are you using a ListView? if so, is it possible that the view is already recycled by ListView (reused to display another list item) by the time that you write the image in it?

Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52
  • See also http://www.youtube.com/watch?v=wDBM6wVEO70 and http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012 . Note, I use the WebImageView as described in http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/ – Marc Van Daele Apr 17 '12 at 07:07
0

Problem happens because you have written the code to load the images in getView() method.

And that method get called every time you scroll the list.

So that's a poor and not good idea at all.

Remove that code from there.. Load images and in other thread or AsyncTask And then call notifyDataSetChanged() of you Adapter class object.

Just steps for what I indicated to do, Please read carefully and try to implement, if cant understand anything feel free to ask..sorry couldnt paste any code snippet..

ListView adapter's getView() method

show default icon if array doesn't contain bitmap at that position
if bitmap is there show bitmap else show default icon

AsyncTask to Load Data

do in background
load data and show in listview

AsyncTask to Load Images

do in background
download images one bye one from the urls got in first AsyncTask
in onPostExecute call adapter's notifyDataSetChanged() to show the bitmaps too

you can also call onProgressUpdate periodically means..after 2-3 image download 
call publishProgress in doInBackground which will call onProgressUpdate and 
in that method also write adpater.notifyDataSetChanged() to 
reflect currently downloaded/available bitmaps in array
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • Where should i put this code after removing from getview() method. How can i set the positions of images for the rows? – sravanthi V Apr 17 '12 at 06:17