1

I have to download some images and display them using Gallery. For the Image adapter I'm using for the gallery I have to start downloading the images in the get view method using an Async task. My problem is that i cant return the downloaded image view to the calling function. I cant download using the main thread due to networkonmainthread exception.

GalleryActivity

public class GalleryActivity extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.gallery);
        ((Gallery) findViewById(R.id.gallery)).setAdapter(new ImageAdapter(this));
     }

Image Adapter

public class ImageAdapter extends BaseAdapter { 

    public View getView(int position, View convertView, ViewGroup parent) {     
        new galleryBackground().execute(Integer.toString(position));
        ImageView i =null;
        return i;
    }

}

Gallery

public class galleryBackground extends AsyncTask<String, Integer, String> { 
  Bitmap bm;    
  public String[] myRemoteImages = { ....};
  ImageView i = new ImageView(GalleryActivity.this);

  @Override
  protected String doInBackground(String... arg0) { 
      try { 
          URL aURL = new URL(myRemoteImages[Integer.parseInt(arg0[0])]);
          URLConnection conn = aURL.openConnection();

          conn.connect();
          InputStream is = conn.getInputStream();
          BufferedInputStream bis = new BufferedInputStream(is);
          bm = bitmapFactory.decodeStream(bis);
          bis.close();
          is.close();   
      }

  @Override     
  protected void onPostExecute(String result) {
     i.setImageBitmap(bm);
     i.setScaleType(ImageView.ScaleType.FIT_CENTER);
     i.setLayoutParams(new Gallery.LayoutParams(150, 150));
     // i have to return this Image view to the calling function        
     super.onPostExecute(result);   
}
Tom
  • 15,798
  • 4
  • 37
  • 48
Manoj Sreekumar
  • 680
  • 3
  • 14
  • 30

4 Answers4

2

This library will solve your problem:

https://github.com/xtremelabs/xl-image_utils_lib-android

Pull the JAR from that repo into your project.

Instantiate an ImageLoader in your Activity/Fragment and pass it in to the adapter.

Call imageLoader.loadImage(imageView, url), and everything is done for you by that system.

The wiki can show you how to plug it in.

WindyB
  • 786
  • 3
  • 9
1

Look at: Lazy load of images in ListView

It does not matter how to display data, your adapter will be the same.

Community
  • 1
  • 1
dilix
  • 3,761
  • 3
  • 31
  • 55
1

you should return bm from doingBackground();

@Override
protected String doInBackground(String... arg0) {
    try{
        URL aURL = new URL(myRemoteImages[Integer.parseInt(arg0[0])]);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = bitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
        return bm;
    }
}
dilix
  • 3,761
  • 3
  • 31
  • 55
user1969053
  • 1,750
  • 1
  • 12
  • 12
  • Some additional information: Sometimes the URLConnection class can hang, so make sure you set a timeout on your URLConnection object. Also, this will retain a reference to your Activity if it becomes destroyed while the image is loading. Consider either switching to the library mentioned below or nulling your ImageView reference in your AsyncTask. – WindyB Jan 21 '13 at 02:20
1

Change your Asynctask to

AsyncTask<String, Integer, Bitmap>

which will return you Bitmap and onPostExecute use the Bitmap You are already passing the position so onPostExecute you can

yourlist.getItem(your position) and set the bitmap 
sheetal
  • 3,014
  • 2
  • 31
  • 45