Requesting images

Content API offers classes for getting images at URL. ImageUrlLabel, ImageLoader, ImageQuery are principal classes for requesting and displaying image efficiently.

ImageUrlLabel

It is an custom ImageLabel that manages image URL loading life cycle. This label manages many features like image size optimization, displaying error and loading images, or avoid unecessary image queries (when label becomes invisible).

Size Optimization

It also provides convenience features like specifying a size to resize to. Its main benefit is that thread scheduling ensures that expensive image operations (decoding, resizing) automatically happen on a background thread.

DefaultImage, ErrorImage

At the beginning ImageLabel shows a default image when loading URL (this image may represent a loading image). If the URL cannot be loaded or read, an error image is displayed (this image may represent a image)

Cancellation

ImageUrlLabel manages canceling pending requests if the view is detached from the hierarchy. This is very useful when an end-user scrolls down a ListView which contains multiple thumbnails.

ImageLoader

ImageLoader is a an orchestrator for large numbers of ImageQuery, for example when putting multiple thumbnails in a ListView. ImageLoader provides an in-memory cache to sit in front of the normal cache, which is important to prevent flickering. This makes it possible to achieve a cache hit without blocking or deferring off the main thread, which is impossible when using disk I/O.

ImageLoader also does response coalescing, without which almost every response handler would set a bitmap on a view and cause a layout pass per image. Coalescing makes it possible to deliver multiple responses simultaneously, which improves performance.

Use ImageUrlLabel and ImageLoader

You can use ImageLoader and ImageUrlLabel in concert to efficiently manage the display of multiple images, such as in a ListView. In your layout XML file, you use ImageUrlLabel in much the same way you would use ImageLabel, for example:

<ImageUrlLabel
id="@+id/imageUrlLabel"
width="60dp"
height="60dp" />

You can use ImageLoader by itself to display an image, for example:

// 20 is the number of image entries to keep in memory
ImageLoader imageLoader = new ImageLoader(new MemoryImageCache(20));
ImageLabel label;

...
label = (ImageUrlLabel) findView(Res.id.imageUrlLabel);

// Get the ImageLoader through your singleton class.
label.setDefaultImage(Res.image.myDefaultImage);
label.setErrorImage(Res.image.myErrorImage);
label.setImageUrl("http://...your image URL...", imageLoader);

mImageLoader = MySingleton.getInstance(this).getImageLoader();
mImageLoader.get(IMAGE_URL, ImageLoader.getImageListener(mImageView,
         R.drawable.def_image, R.drawable.err_image));