Kuwame Brown
Kuwame Brown

Reputation: 533

How to load images from server properly?

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;
 }

Upvotes: 0

Views: 1366

Answers (2)

Brian Cooley
Brian Cooley

Reputation: 11662

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.

Upvotes: 0

Blundell
Blundell

Reputation: 76458

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.

Upvotes: 2

Related Questions