-1

I am fetching images from URL. I use AsyncTask to display images. But images reload each time when I scroll up and down.

Here is my code.

public View getView(final int position, View convertView,
        ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.useryouramourstlist, null,
            true);

    final ImageView userprofilepic = (ImageView) view
            .findViewById(R.id.userprofilepic);

    try
    {

        new ImageLoader().execute(view, +URL+listArrphoto[position]);

    }
    catch(Exception e)
    {

    }

    return view;
}


public class ImageLoader extends AsyncTask<Object, String, Bitmap> {

    private View view;
    private Bitmap bm = null;

    @Override
    protected Bitmap doInBackground(Object... parameters) {

        // Get the passed arguments here
        view = (View) parameters[0];
        String uri = (String)parameters[1];


        bm = loadImageFromUrl(uri);

        return bm;
        //return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null && view != null) {
            ImageView albumArt = (ImageView) view.findViewById(R.id.userprofilepic);
            albumArt.setImageBitmap(bitmap);
        }
    }
}

public static Bitmap loadImageFromUrl(String url) {

    Bitmap bm;
    try {  

            URL aURL = new URL(url);  
            URLConnection conn = aURL.openConnection(); 

            conn.connect();  
            InputStream is = null;
            try
            {
             is= conn.getInputStream();  
            }catch(IOException e)
            {
                 return null;
            }

            BufferedInputStream bis = new BufferedInputStream(is);  

            bm = BitmapFactory.decodeStream(bis);

            bis.close();  
            is.close();  

       } catch (IOException e) {  
        return null;
       }  

    return  Bitmap.createScaledBitmap(bm,60,60,true);


}
Aleks G
  • 56,435
  • 29
  • 168
  • 265
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
  • you need to cache images if want to avoid downloading it everytime. Use LazyList of universal image loader. Check the link http://stackoverflow.com/questions/15621936/whats-lazylist – Raghunandan May 01 '13 at 13:51
  • But Why images load every time when scroll up and down. If I used without AsyncTask then Its not load again and again when scroll up or down – Nirav Ranpara May 01 '13 at 13:52
  • you might be calling asynctask in your getview. – Raghunandan May 01 '13 at 13:54
  • can you say me how to do that ? – Nirav Ranpara May 01 '13 at 13:55
  • how to use lazy list or UIL check the link in the 1st comment. It caches images in sccard or phone memory. If already present , displat from cache else download cache and display – Raghunandan May 01 '13 at 13:56

1 Answers1

0

Another easy option, though could be costly on memory depending on your list size, is on your ImageView after loading the image call view.setTag(bitmap), and before you attempt to fetch the image, do a sanity check.

if(view.getTag() != null) {
    view.setImageBitmap((Bitmap)view.getTag());
} else {
    new ImageLoader().execute(view, +URL+listArrphoto[position]);
}

Of course, I would opt for downloading to the applications cache directory and loading from the file if it exists.

vincentjames501
  • 1,018
  • 11
  • 15
  • You can also try storing it into a Map where the key is the url and the value is the Bitmap. Again, I would not recommend this for large lists. – vincentjames501 May 01 '13 at 14:11