1

I am trying to display the images in galleryViews which are in ListView. I am downloading images from server to SDCard and then displaying it, first I am checking images in cache if image is not there then I am loading it from sdcard.

when the user starts the app for the first time I am downloading the images from server and saving them to sdcard in the mean time iam showing the activity with text only

I want if the image is not there in sdcard the after downloading the image it should display the image as soon as the image is download here is what I am doing.

public class AsyncImageLoader {
   private boolean isImageView;
   private final LinkedHashMap<String, Bitmap> cache = 
      new LinkedHashMap<String, Bitmap>(60, (float) 1.0, true);


Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
  super.handleMessage(msg);

  LoadImageFromSdCard loadImage = (LoadImageFromSdCard) msg.obj;
  if (isImageView) {
    loadImage.imageView.setImage(loadImage.bmp);
  } else {
     Thread thread = new Thread(new LoadImageFromSdCard(loadImage.uri,  loadImage.imageView));
     try {
         Log.i("AsyncImageLoader", "SECOND THREAD STARTED");
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
     thread.start();
   }
  }
};


public Bitmap loadImage(String uri,
  ImageTextComboControl imageView) {
if (cache.containsKey(uri)) {
  return cache.get(uri);
} else {
  handler.post(new LoadImageFromSdCard(uri, imageView));
}

return null;
}

private class LoadImageFromSdCard implements Runnable {

 String uri;    
 ImageTextComboControl imageView;
 ImageView image;
 Bitmap bmp = null;

 public LoadImageFromSdCard(String uri, ImageTextComboControl imageView) {
  this.uri = uri;
  this.imageView = imageView;
 }

public void run() {
  FileInputStream fis;
  try {
    fis = new FileInputStream(new File(uri));
  } catch (FileNotFoundException e) {
    e.printStackTrace();
    return;
  }
  bmp = BitmapFactory.decodeStream(fis);
  if (imageView != null) {
    isImageView = true;
    cache.put(uri, bmp);
    Message message = new Message();
    message.obj = this;
    handler.sendMessage(message);
  } 

  }
 }
}

Thanx

2 Answers2

1

Is much better use Async Task class http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html (using AsyncTask section) and, important, take care about large bitmap

http://developer.android.com/training/displaying-bitmaps/index.html

Aracem
  • 7,227
  • 4
  • 35
  • 72
  • my problem is images are downloading on separate thread to sdcard as soon as image is downloaded I want to display it in image view from sdcard. – Surendra Reddy May 29 '12 at 11:02
  • I answer you. See Async Task manual. It has a method, onPostExecute, that run in the UIThread when the main thread proccess, doInBackground, finish. If the answer its correct, please vote up ;) – Aracem May 29 '12 at 14:13
  • thanks, I solved my problem using onContentChanged() method in adapter. But your suggestion helped me in understanding and handling the images thanks a lot. – Surendra Reddy Jun 05 '12 at 09:36
1

I solved my problem using onContentChanged() method in adapter. I am saving images to sdCard and getting the sdCard path saving it into sqlite database so when ever the data is changing in sqlite database onContentChanged method is called because I am using Cursor Adapter.