Skip to content

This project aims to provide a reusable instrument for asynchronous image loading, caching and displaying.

License

Notifications You must be signed in to change notification settings

KevinChen9117/Android-Universal-Image-Loader

 
 

Repository files navigation

Universal Image Loader for Android

1 year with you :)

This project aims to provide a reusable instrument for asynchronous image loading, caching and displaying. It is originally based on Fedor Vlasov's project and has been vastly refactored and improved since then.

Download: JAR library; sources (you can attach it to project as source attachment so you can see Java docs)

Screenshot

Features

  • Multithread image loading
  • Possibility of wide tuning ImageLoader's configuration (thread pool size, HTTP options, memory and disc cache, display image options, and others)
  • Possibility of image caching in memory and/or on device's file sysytem (or SD card)
  • Possibility to "listen" loading process
  • Possibility to customize every display image call with separated options
  • Widget support

Android 1.5+ support

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 bit outdated

First look at Useful Info.

If you have some question or problems (including OOM) about Universal Image Loader you can ask it on StackOverFlow with [universal-image-loader] tag. Also add [java] and [android] tags. Mention next information in your question/problem:

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

Bugs and feature requests place here.

Usage

Simple

ImageView imageView = ...
String imageUrl = "http://site.com/image.png"; // or "file:///mnt/sdcard/images/image.jpg"

// Get singletone instance of ImageLoader
ImageLoader imageLoader = ImageLoader.getInstance();
// Initialize ImageLoader with configuration. Do it 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.jpg"
ProgressBar spinner = ...
File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "UniversalImageLoader/Cache");

// Get singletone instance of ImageLoader
ImageLoader imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional, use only those you really want to customize)
// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of using ALL options. Most 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, use it carefully (Better don't use it)
			.threadPoolSize(3)
			.threadPriority(Thread.NORM_PRIORITY - 1)
			.denyCacheImageMultipleSizesInMemory()
			.offOutOfMemoryHandling()
			.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation
			.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own 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 created configuration. Do it once on Application start.
imageLoader.init(config);
// Creates display image options for custom display task (all options are optional)
DisplayImageOptions options = new DisplayImageOptions.Builder()
           .showStubImage(R.drawable.stub_image)
		   .showImageForEmptyUri(R.drawable.image_for_empty_url)
		   .resetViewBeforeLoading()
           .cacheInMemory()
           .cacheOnDisc()
		   .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_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 onLoadingStarted() {
       spinner.show();
    }
	@Override
	public void onLoadingFailed(FailReason failReason) {
		spinner.hide();
	}
    @Override
    public void onLoadingComplete(Bitmap loadedImage) {
        spinner.hide();
    }
	@Override
    public void onLoadingCancelled() {
        // Do nothing
    }
});
// 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() {
	@Override
	public void onLoadingComplete(Bitmap loadedImage) {
		// Do whatever you want with loaded Bitmap
	}
});

Useful info

  1. Caching is NOT enabled by default. If you want loaded images will be cached in memory and/or on disc then you should enable caching in DisplayImageOptions this way:
// Create default options which will be used for every 
//  displayImage(...) call if no options will be passed to this method
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
			...
            .cacheInMemory()
            .cacheOnDisc()
            ...
            .build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            ...
            .defaultDisplayImageOptions(defaultOptions)
            ...
            .build();
ImageLoader.getInstance().init(config); // Do it on Application start
// Then later, when you want to display image
ImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used

or this way:

DisplayImageOptions options = new DisplayImageOptions.Builder()
			...
            .cacheInMemory()
            .cacheOnDisc()
            ...
            .build();
ImageLoader.getInstance().displayImage(imageUrl, imageView, options); // Incoming options will be used
  1. If you enabled disc caching then UIL try to cache images on external storage (/sdcard/Android/data/[package_name]/cache). If external storage is not available then images are cached on device's filesytem. To provide caching on external storage (SD card) add following permission to AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  1. How UIL define Bitmap size needed 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 try to set android:layout_width|android:layout_height or android:maxWidth|android:maxHeight parameters for ImageView if you know approximate maximum size of it. It will help correctly compute Bitmap size needed for this view and save memory.

  1. If you often got OutOfMemoryError in your app using Universal Image Loader then try next (all of them or several):
  • Reduce thread pool size in configuration (.threadPoolSize(...)). 1 - 5 is recommended.
  • Use .bitmapConfig(Bitmap.Config.RGB_565) in display options. Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888.
  • Use .memoryCache(new WeakMemoryCache()) in configuration or disable caching in memory at all in display options (don't call .cacheInMemory()).
  • Use .imageScaleType(ImageScaleType.IN_SAMPLE_INT) in display options. Or try .imageScaleType(ImageScaleType.EXACTLY).
  • Avoid using RoundedBitmapDisplayer. It creates new Bitmap object with ARGB_8888 config for displaying during work.
  1. For memory cache configuration (ImageLoaderConfiguration.Builder.memoryCache(...)) you can use already prepared implementations:
  • UsingFreqLimitedMemoryCache (The least frequently used bitmap is deleted when cache size limit is exceeded) - Used by default
  • LRULimitedMemoryCache (Least recently used bitmap is deleted when cache size limit is exceeded)
  • FIFOLimitedMemoryCache (FIFO rule is used for deletion when cache size limit is exceeded)
  • LargestLimitedMemoryCache (The largest bitmap is deleted when cache size limit is exceeded)
  • LimitedAgeMemoryCache (Decorator. Cached object is deleted when its age exceeds defined value)
  • WeakMemoryCache (Memory cache with only weak references to bitmaps)
  1. For disc cache configuration (ImageLoaderConfiguration.Builder.discCache(...)) you can use already prepared implementations:
  • UnlimitedDiscCache (The fastest cache, doesn't limit cache size) - Used by default
  • TotalSizeLimitedDiscCache (Cache limited by total cache size. If cache size exceeds specified limit then file with the most oldest last usage date will be deleted)
  • FileCountLimitedDiscCache (Cache limited by file count. If file count in cache directory exceeds specified limit then file with the most oldest last usage date will be deleted. Use it if your cached files are of about the same size.)
  • LimitedAgeDiscCache (Size-unlimited cache with limited files' lifetime. If age of cached file exceeds defined limit then it will be deleted from cache.)

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

  1. To display bitmap (DisplayImageOptions.Builder.displayer(...)) you can use already prepared implementations:
  • RoundedBitmapDisplayer (Displays bitmap with rounded corners)
  • FadeInBitmapDisplayer (Displays image with "fade in" animation)

Applications using Universal Image Loader

MediaHouse, UPnP/DLNA Browser | Деловой Киров | Бизнес-завтрак | Menu55 | SpokenPic | Kumir | EUKO 2012 | TuuSo Image Search | Газета Стройка | Prezzi Benzina (AndroidFuel) | [Quiz Guess The Guy] (https://play.google.com/store/apps/details?id=com.game.guesstheguy) | Volksempfänger (alpha) | ROM Toolbox Lite, Pro | London 2012 Games | 카톡 이미지 - 예쁜 프로필 이미지 | dailyPen | TK App | Mania! | Stadium Astro | Chef Astro | Lafemme Fashion Finder | FastPaleo | Live Soccer Scores | friendizer | LowPrice lowest book price | bluebee | Game PromoBox | EyeEm - Photo Filter Camera | Festival Wallpaper | Gaudi Hall | Spocal | PhotoDownloader for Facebook | Вкладыши | Dressdrobe | mofferin | WordBoxer | EZ Imgur | Ciudad en línea | Urbanismo en línea | Waypost | Moonrise Kingdom Wallpapers HD | Chic or Shock? | Auto Wallpapers | Heyou | Brasil Notícias | ProfiAuto’s VideoBlog | CarteleraApp (Cine), AdsFree | Listonic - Zamów Zakupy | Topface - meeting is easy | Name The Meme | Name The World | Pregnancy Tickers - Widget | Hindi Movies & More | Telugu Movies & More | Ram Charan HD Wallpaper | Jessica Alba HD Wallpaper | User Manager ROOT Android 4.2

Donation

You can support the project and thank the author for his hard work :)

License

Copyright (c) 2011-2012, Sergey Tarasevich

If you use Universal Image Loader code in your application you must inform the author about it ( email: nostra13[at]gmail[dot]com ) like this:

I use Universal Image Loader in [ApplicationName] - http://link_to_google_play. I [allow|don't allow] to mention my app in "Applications using Universal Image Loader" on GitHub.

Also you should mention it (but it is not required) in application UI with string "Used Universal-Image-Loader (c) 2011-2012, Sergey Tarasevich" (e.g. in some "About" section).

Licensed under the BSD 3-clause

About

This project aims to provide a reusable instrument for asynchronous image loading, caching and displaying.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published