0

I have a code below, this code works perfectly fine when you are trying to load one or two images. However, the problem is, when you have a lot of images in one Activity, all images must load first before you can get in. My question is, what is the best way to handle this images? I want the images to load one by one and not simultaneously. I want it to load just like the ListView made by Fedor "http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview" (Note: I'm not using a ListView, I just want my images to load like that). Please help me, I would really appreciate it a lot. Thanks in advance!

 class ImageDownlaodThread extends Thread {
  ImageDownloadMessageHandler imageDownloadMessageHandler;
  String imageUrl;



  @Override
  public void run() {
   Drawable drawable = LoadImageFromWebOperations(imageUrl);
   Message message = imageDownloadMessageHandler.obtainMessage(1, drawable);
   System.out.println("Message sent");
  }

 }

 class ImageDownloadMessageHandler extends Handler {
  View imageTextView;

  }

  @Override
  public void handleMessage(Message message) {
   progressBar.setVisibility(View.GONE);
   imageTextView.setVisibility(View.VISIBLE);
  }

 }

 Drawable LoadImageFromWebOperations(String url) {
  Drawable d = null;
  InputStream is = null;
  try {

  } catch (IOException e) {
   e.printStackTrace();
  }
  return d;
 }
Kuwame Brown
  • 533
  • 1
  • 10
  • 22
  • You should clean your code listing up some. It's not clear what you are doing. There's nothing in your try/catch block. I can't see how this would work perfectly for one or two images. – Brian Cooley Mar 30 '11 at 20:44

2 Answers2

2

Would this tutorial help [TUT] ImageView with Loading Spinner:

http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html

It basically show's a spinner until the image has loaded from the remote site :-)

You could strip the code to show a blank space / whatever you want till it loads.

If you don't want anything to happen till it all loads, you could have a count of how many images there are and then increment this each time an image has loaded.

Blundell
  • 75,855
  • 30
  • 208
  • 233
0

IMO, AsyncTask is the easiest way to do this; in fact, it was basically built for this sort of task.

There's really too much to discuss in a StackOverflow answer. Just see Painless Threading to get started.

Brian Cooley
  • 11,622
  • 4
  • 40
  • 39
  • Can you just help me to modify my code in order to accomplish my goal? – Kuwame Brown Mar 29 '11 at 15:42
  • Your code doesn't show what you are doing. In particular, it is not clear how you are accessing multiple image files. Your code is for creating one Handler and launching one Thread to access a single image URL. Are you just launching a bunch of Handlers in your Activity code? – Brian Cooley Mar 29 '11 at 17:04
  • actually yes, I am just launching a bunch of Handlers, because I don't know how to create a handle for multiple images – Kuwame Brown Mar 30 '11 at 07:00