AndroidImageLoader_ Loading Images Asynchronously on Android _ Wu-Man's Blog

4
5/13/2014 AndroidImageLoader: Loading Images Asynchronously on Android | Wu-Man's Blog http://blog.wu-man.com/2012/10/androidimageloader-loading-images.html 1/4 31st October 2012 Almost every Android application has a need to bind remote images to a View [http://developer.android.com/reference/android/view/View.html] on screen. A very common usage is to load a bunch of remote images, cache them for later reuse, and finally bind them to each view in a scrolling AdapterView [http://developer.android.com/reference/android/widget/AdapterView.html] . Making this process efficient and memory-leak-free, however, is not exactly trivial. There are at least these performance considerations: All the IO activities need to be done in a background thread to avoid blocking the main thread. Images that fail to be loaded (usually due to network disconnection or missing resource) should be remembered so that unnecessary retrying requests are not repeated. Loading requests should be executed in a LIFO manner to improve perceived response time. Loaded images should be cached at least in memory so that future requests for the same image can be loaded faster. Asynchronously binding images to an AdapterView can be tricky. Calling BaseAdapter.notifyDataSetChanged() [http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSe tChanged()] upon callback can be a very expensive operation. On the other hand, a major memory consideration is to avoid memory leaks. Since image loading must be done in an asynchronous manner, views holding a strong reference to Context [http://developer.android.com/reference/android/content/Context.html] inevitably have to be kept and referenced by the image loader itself. This can best be prevented by using a WeakReference [http://developer.android.com/reference/java/lang/ref/WeakReference.html] . Loaded images are to be kept in cache, which can potentially cause a memory leak if Drawable s [http://developer.android.com/reference/android/graphics/drawable/Drawable.html] are cached rather than Bitmap s [http://developer.android.com/reference/android/graphics/Bitmap.html] . To address these issues, I have forked the Image Loader [http://code.google.com/p/libs-for-android/wiki/ImageLoader] component of the libs-for- android [http://code.google.com/p/libs-for-android/] project to create the AndroidImageLoader library. AndroidImageLoader: Loading Images Asynchronously on Android

description

AndroidImageLoader_ Loading Images Asynchronously on Android _ Wu-Man's Blog

Transcript of AndroidImageLoader_ Loading Images Asynchronously on Android _ Wu-Man's Blog

5/13/2014 AndroidImageLoader: Loading Images Asynchronously on Android | Wu-Man's Blog

http://blog.wu-man.com/2012/10/androidimageloader-loading-images.html 1/4

31st October 2012

Almost every Android application has a need to bind remote images to a View

[http://developer.android.com/reference/android/view/View.html] on screen. A very

common usage is to load a bunch of remote images, cache them for later reuse,

and finally bind them to each view in a scrolling AdapterView

[http://developer.android.com/reference/android/widget/AdapterView.html] .

Making this process efficient and memory-leak-free, however, is not exactly trivial.

There are at least these performance considerations:

All the IO activities need to be done in a background thread to avoid blocking

the main thread.

Images that fail to be loaded (usually due to network disconnection or missing

resource) should be remembered so that unnecessary retrying requests are

not repeated.

Loading requests should be executed in a LIFO manner to improve perceived

response time.

Loaded images should be cached at least in memory so that future requests for

the same image can be loaded faster.

Asynchronously binding images to an AdapterView can be tricky. Calling

BaseAdapter.notifyDataSetChanged()

[http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSe

tChanged()] upon callback can be a very expensive operation.

On the other hand, a major memory consideration is to avoid memory leaks.

Since image loading must be done in an asynchronous manner, views holding

a strong reference to Context

[http://developer.android.com/reference/android/content/Context.html] inevitably have

to be kept and referenced by the image loader itself. This can best be

prevented by using a WeakReference

[http://developer.android.com/reference/java/lang/ref/WeakReference.html] .

Loaded images are to be kept in cache, which can potentially cause a memory

leak if Drawables

[http://developer.android.com/reference/android/graphics/drawable/Drawable.html] are

cached rather than Bitmaps

[http://developer.android.com/reference/android/graphics/Bitmap.html] .

To address these issues, I have forked the Image Loader

[http://code.google.com/p/libs-for-android/wiki/ImageLoader] component of the libs-for-

android [http://code.google.com/p/libs-for-android/] project to create the

AndroidImageLoader library.

AndroidImageLoader: LoadingImages Asynchronously on Android

5/13/2014 AndroidImageLoader: Loading Images Asynchronously on Android | Wu-Man's Blog

http://blog.wu-man.com/2012/10/androidimageloader-loading-images.html 2/4

[https://raw.github.com/wuman/AndroidImageLoader/master/library/src/site/static/feature.pn

g]

The AndroidImageLoader is an Android library that helps to load images

asynchronously. Like its upstream libs-for-android project, it executes image

requests in a thread pool and provides caching support. The following features of

libs-for-android are kept:

Images are downloaded and saved to cache via a pool of background threads.

Supports preloading of off-screen images into a memory cache.

Supports prefetching of images into a disk cache.

HttpUrlConnection

[http://developer.android.com/reference/java/net/HttpURLConnection.html] is used for

loading images, which respects cache control.

Custom URLStreamHandlerFactory

[http://developer.android.com/reference/java/net/URLStreamHandlerFactory.html] is

supported for creating connections to special URLs such as content:// URIs.

Supports Bitmap transformations by accepting a ContentHandler

[http://developer.android.com/reference/java/net/ContentHandler.html] for loading

images.

The AndroidImageLoader improves libs-for-android in the following ways:

In addition to memory cache, the library also supports second level disk

caching. Caching support in AndroidImageLoader is now made to depend on

the TwoLevelLruCache [http://wuman.github.com/TwoLevelLruCache/] library,

which in turn depends on the more widely used LruCache

[http://developer.android.com/reference/android/util/LruCache.html] and

DiskLruCache [https://github.com/JakeWharton/DiskLruCache] .

Image requests are now placed into a LIFO task queue, which makes more

sense in most scrolling scenarios.

The API for view binding is now in a separate ViewBinder class. Applications

can use the ImageViewBinder class to bind to ImageViews or extend the

AbstractViewBinder class for custom views. Also, ImageViews within an

AdapterView no longer require a different kind of binding.

OutOfMemoryErrors are automatically detected and caught. When memory is

running low, the cache size is automatically decreased to give back more

memory to the system.

5/13/2014 AndroidImageLoader: Loading Images Asynchronously on Android | Wu-Man's Blog

http://blog.wu-man.com/2012/10/androidimageloader-loading-images.html 3/4

Prefetching can now be supported out of the box via the SinkContentHandler

and the HttpResponseCache [https://github.com/candrews/HttpResponseCache]

library.

The library is open source and hosted on GitHub

[http://wuman.github.com/AndroidImageLoader/] . It is well documented and has

samples that illustrate the use of the exposed API. Feel free to try it out.

Posted 31st October 2012 by David Wu

Labels: android, asynchronous, cache, google, image, imageloader, jank, leaks,library, libs-for-android, memory, opensource, pattern, performance, reader, ui

Replies

Reply

6 View comments

Niqo 1:27 AM

Thanks for sharing this. This lib is exactly what I was looking for (using Jakewharton DiskLRUCache, Bitmap transformation, fadein, ...). I will investigate itsoon.

Reply

Chung-Ming Ts'ai 11:05 AM

Hi, I'm trying to use this library but find the dependency tree is quite deep. Is itpossible to package all the dependent libraries into an archive?

Reply

David Wu 4:31 PM

Sure I'll work on that when I have some free time next week.

San Kira 1:39 AM

Please check GitHub link ...

Reply

Anonymous 1:44 AM

Please check the provided link for GitHub....

Reply

Tarnoju Upendra 7:48 PM

Github link not opening up

Reply

5/13/2014 AndroidImageLoader: Loading Images Asynchronously on Android | Wu-Man's Blog

http://blog.wu-man.com/2012/10/androidimageloader-loading-images.html 4/4

Enter your comment...

Comment as: Google Account

Publish

Preview

New Ssangyong Rexton RX6ssangyongrexton.in

High-End Manual Transmission Model by Mahindra. Know More Now!