Android universal image loader

Android-universal-image-loader-featured.jpg

Features

Multithread image loading
Chance of wide tuning ImageLoader’s configuration (thread pool size, HTTP options, memory and disc cache, display image options, yet others)
Chance of image caching in memory and/or on device’s file sysytem (or Sdcard)
Possible ways to “listen” loading process
Possible ways to personalize every display image call with separated options
Widget support

Android 1.5+ support

Android universal image loader1 Android universal image loader

Documentation*

Universal Image Loader. Part 1 – Introduction [RU EN]
Universal Image Loader. Part 2 – Configuration [RU EN]
Universal Image Loader. Part 3 – Usage [RU EN]

(*) a little outdated
User Support

Start looking at Helpful Info.

For those who have some question or problems (including OOM) about Universal Image Loader you are able to request it on StackOverFlow with [universal-image-loader] tag. Include [java] and [android] tags. Mention next information inside your question/problem:

your configuration (ImageLoaderConfiguration)
display options (DisplayImageOptions)
getView() method code of the adapter (if you are using it)
XML layout of the ImageView you load image into

Bugs and have demands place here.
Changelog
Usage
Simple

ImageView imageView = …
String imageUrl = “http://site.com/image.png” // or “file:///mnt/sdcard/images/image.digital”

// Get singletone demonstration of ImageLoader
ImageLoader imageLoader = ImageLoader.getInstance()
// Initialize ImageLoader with configuration. Get it done once.
imageLoader.init(ImageLoaderConfiguration.createDefault(context))
// Load and display image asynchronously
imageLoader.displayImage(imageUrl, imageView)

Most detailed

ImageView imageView = …
String imageUrl = “http://site.com/image.png” // or “file:///mnt/sdcard/images/image.digital”
ProgressBar spinner = …
File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), “UniversalImageLoader/Cache”)

// Get singletone demonstration of ImageLoader
ImageLoader imageLoader = ImageLoader.getInstance()
// Create configuration for ImageLoader (all choices are optional, only use individuals you want to personalize)
// DON’T COPY THIS CODE For Your PROJECT! Case illustration of using ALL options. Many of them have default values.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.memoryCacheExtraOptions(480, 800) // max width, max height
.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75) // Can slow ImageLoader, utilize it carefully (Better avoid using it)
.threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY – 1)
.denyCacheImageMultipleSizesInMemory()
.offOutOfMemoryHandling()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You are able to pass your personal memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You are able to pass your personal disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new URLConnectionImageDownloader(5 * 1000, 20 * 1000)) // connectTimeout (5 s), readTimeout (20 s)
.tasksProcessingOrder(QueueProcessingType.FIFO)
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.enableLogging()
.build()
// Initialize ImageLoader with produced configuration. Get it done once on Application start.
imageLoader.init(config)

// Produces display image choices for custom display task (all choices are optional)
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_Energy_OF_2)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.delayBeforeLoading(1000)
.displayer(new RoundedBitmapDisplayer(20))
.build()
// Load and display image
imageLoader.displayImage(imageUrl, imageView, options, new ImageLoadingListener()
@Override
public void onLoadingFailed(FailReason failReason)
@Override
public void onLoadingComplete(Bitmap loadedImage)
@Override
public void onLoadingCancelled() Do nothing at all

)

// Just load image
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.displayer(new FakeBitmapDisplayer())
.build()
ImageSize minImageSize = new ImageSize(120, 80)
imageLoader.loadImage(context, imageUrl, minImageSize, options, new SimpleImageLoadingListener() anything you want with loaded Bitmap

)

Helpful info

Caching isn’t enabled automatically. If you would like loaded images is going to be cached in memory and/or on disc then you need to enable caching in DisplayImageOptions by doing this:

// Create default options which is employed for every
// displayImage(…) call if no options is going to be passed for this method
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()

.cacheInMemory()
.cacheOnDisc()

.build()
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())

.defaultDisplayImageOptions(defaultOptions)

.build()
ImageLoader.getInstance().init(config) // Get it done on Application start

// Then later, when you wish to show image
ImageLoader.getInstance().displayImage(imageUrl, imageView) // Default options is going to be used

or by doing this:

DisplayImageOptions options = new DisplayImageOptions.Builder()

.cacheInMemory()
.cacheOnDisc()

.build()
ImageLoader.getInstance().displayImage(imageUrl, imageView, options) // Incoming options is going to be used

Should you enabled disc caching then UIL attempt to cache images on exterior storage (/sdcard/Android/data/[package_title]/cache). If exterior storage isn’t available then images are cached on device’s filesytem. To supply caching on exterior storage (Sdcard) add following permission to AndroidManifest.xml:

How UIL define Bitmap size required for exact ImageView? It searches defined parameters:
Get android:layout_width and android:layout_height parameters
Get android:maxWidth and/or android:maxHeight parameters
Get maximum width and/or height parameters from configuration (memoryCacheExtraOptions(int, int) option)
Get width and/or height of device screen

So attempt to set android:layout_widthandroid:layout_height or android:maxWidthandroid:maxHeight parameters for ImageView knowing approximate maximum size it. It can help properly compute Bitmap size required for this view and save memory.

Should you frequently got OutOfMemoryError inside your application using Universal Image Loader then try set WeakMemoryCache into configuration:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())

.memoryCache(new WeakMemoryCache())

.build()

or disable caching in memory whatsoever (in DisplayImageOptions).

For memory cache configuration (ImageLoaderConfiguration.Builder.memoryCache(…)) you should use ready to eat implementations:
UsingFreqLimitedMemoryCache (Minimal commonly used bitmap is erased when cache size limit is exceeded) – Used automatically
LRULimitedMemoryCache (Least lately used bitmap is erased when cache size limit is exceeded)
FIFOLimitedMemoryCache (FIFO rule can be used for deletion when cache size limit is exceeded)
LargestLimitedMemoryCache (The biggest bitmap is erased when cache size limit is exceeded)
LimitedAgeMemoryCache (Decorator. Cached object is erased when wear and tear surpasses defined value)
WeakMemoryCache (Memory cache with only weak references to bitmaps)

For disc cache configuration (ImageLoaderConfiguration.Builder.discCache(…)) you should use ready to eat implementations:
UnlimitedDiscCache (The quickest cache, does not limit cache size) – Used automatically
TotalSizeLimitedDiscCache (Cache restricted to total cache size. If cache size surpasses specified limit then file most abundant in earliest last usage date is going to be erased)
FileCountLimitedDiscCache (Cache restricted to file count. If file count in cache directory surpasses specified limit then file most abundant in earliest last usage date is going to be erased. Utilize it in case your cached files are of comparable size.)
LimitedAgeDiscCache (Size-limitless cache with limited files’ lifetime. If chronilogical age of cached file surpasses defined limit then it will likely be erased from cache.)

NOTE: UnlimitedDiscCache is 30%-faster than other limited disc cache implementations.

To show bitmap (DisplayImageOptions.Builder.displayer(…)) you should use ready to eat implementations:
RoundedBitmapDisplayer (Shows bitmap with rounded corners)
FadeInBitmapDisplayer (Shows image with “fade in” animation)

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)