Andorid library that loads images asynchronously into cache using a thread pool

Overview

AndroidImageLoader

Feature Image

AndroidImageLoader is a fork of the Image Loader component in libs-for-android.

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 is used for loading images, which respects cache control.
  • Custom URLStreamHandlerFactory is supported for creating connections to special URLs such as content:// URIs.
  • Supports Bitmap transformations by accepting a ContentHandler 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 library, which in turn depends on the more widely used LruCache and 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.
  • Prefetching can now be supported out of the box via the SinkContentHandler and the HttpResponseCache library.

USAGE

The ImageLoader should be installed as a pseudo-system service. This is so that it can be used as a singleton and accessed across Activities. You do this by declaring a customized Application in the AndroidManifest.xml:

public class SamplesApplication extends Application {
    private ImageLoader mImageLoader;

    @Override public void onCreate() {
        super.onCreate();
        try {
            mImageLoader = createImageLoader(this);
        } catch (IOException e) {
        }
    }

    @Override public void onTerminate() {
        mImageLoader = null;
        super.onTerminate();
    }

    @Override public Object getSystemService(String name) {
        if (ImageLoader.IMAGE_LOADER_SERVICE.equals(name)) {
            return mImageLoader;
        } else {
            return super.getSystemService(name);
        }
    }
    
    ...
}

An example of creating the ImageLoader instance can be:

private static ImageLoader createImageLoader(Context context)
        throws IOException {
    ContentHandler prefetchHandler = null;
    try {
        HttpResponseCache.install(
                new File(context.getCacheDir(), "HttpCache"),
                ImageLoader.DEFAULT_CACHE_SIZE * 2);
        prefetchHandler = new SinkContentHandler();
    } catch (Exception e) {
    } 

    // Use a custom URLStreamHandlerFactory if special URL scheme is needed
    URLStreamHandlerFactory streamFactory = null;

    // Load images using the default BitmapContentHandler
    ContentHandler bitmapHandler = new BitmapContentHandler();
    bitmapHandler.setTimeout(5000);
    
    return new ImageLoader(
        streamFactory, bitmapHandler, prefetchHandler,
        ImageLoader.DEFAULT_CACHE_SIZE, 
        new File(context.getCacheDir(), "images"));        
}

Obtaining the ImageLoader from within an Activity is easy:

ImageLoader imageLoader = ImageLoader.get(context);

Binding an image to ImageView is done via ImageViewBinder:

ImageViewBinder binder = new ImageViewBinder(mImageLoader);
binder.bind(imageView, url);

Optionally you can set alternative images to display:

binder.setLoadingResource(R.drawable.loading);
binder.setErrorResource(R.drawable.unavailable);

By default images loaded from disk cache or network are faded in. You can disable this special effect by:

binder.setFadeIn(false);

Note that the ViewBinder automatically checks whether the target View is still requesting the same URL or already recycled to request another image.

Custom view binding is supported by extending the AbstractViewBinder<V> class and overriding at least the following two methods:

  • void onImageLoaded(V targetView, Bitmap bitmap, String url, LoadSource loadSource)
  • void onImageError(V targetView, String url, Throwable error)

SAMPLES

Sample applications are provided to better understand how to use the library.

  • ImageViewBinding: loading of images using the ImageViewBinder.
  • CustomViewBinding: loading of images into custom views by extending the AbstractViewBinder class.
  • FlickrInterestingness: practical example of loading Flickr images from its Web API using the AndroidImageLoader in combination with the AndroidFeedLoader.

INCLUDING IN YOUR PROJECT

There are two ways to include AndroidImageLoader in your projects:

  1. You can download the released jar file in the Downloads section.

  2. If you use Maven to build your project you can simply add a dependency to this library.

     <dependency>
         <groupId>com.wu-man</groupId>
         <artifactId>androidimageloader-library</artifactId>
         <version>0.1</version>
     </dependency>
    

CONTRIBUTE

If you would like to contribute code to AndroidImageLoader you can do so through GitHub by forking the repository and sending a pull request.

DEVELOPED BY

LICENSE

Copyright 2012 David Wu
Copyright (C) 2010 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and 
limitations under the License.

Bitdeli Badge

You might also like...
A powerful image downloading and caching library for Android
A powerful image downloading and caching library for Android

Picasso A powerful image downloading and caching library for Android For more information please see the website Download Download the latest AAR from

Photo picker library for android. Let's you pick photos directly from files, or navigate to camera or gallery.
Photo picker library for android. Let's you pick photos directly from files, or navigate to camera or gallery.

ChiliPhotoPicker Made with ❤️ by Chili Labs. Library made without DataBinding, RxJava and image loading libraries, to give you opportunity to use it w

An Android transformation library providing a variety of image transformations for Glide.
An Android transformation library providing a variety of image transformations for Glide.

Glide Transformations An Android transformation library providing a variety of image transformations for Glide. Please feel free to use this. Are you

An android image compression library.
An android image compression library.

Compressor Compressor is a lightweight and powerful android image compression library. Compressor will allow you to compress large photos into smaller

An Android transformation library providing a variety of image transformations for Picasso
An Android transformation library providing a variety of image transformations for Picasso

Picasso Transformations An Android transformation library providing a variety of image transformations for Picasso. Please feel free to use this. Are

Library to handle asynchronous image loading on Android.

WebImageLoader WebImageLoader is a library designed to take to hassle out of handling images on the web. It has the following features: Images are dow

Compose Image library for Kotlin Multiplatform.

Compose ImageLoader Compose Image library for Kotlin Multiplatform. Setup Add the dependency in your common module's commonMain sourceSet kotlin {

It is far easier to design a class to be thread-safe than to retrofit it for thread safety later
It is far easier to design a class to be thread-safe than to retrofit it for thread safety later

"It is far easier to design a class to be thread-safe than to retrofit it for thread safety later." (Brian Goetz - Java concurrency: Publisher: Addiso

TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images.  Plugins are available for features like markers, hotspots, and path drawing.
TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.

This project isn't maintained anymore. It is now recommended to use https://github.com/peterLaurence/MapView. MapView is maintained by Peter, one of o

TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images.  Plugins are available for features like markers, hotspots, and path drawing.
TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.

This project isn't maintained anymore. It is now recommended to use https://github.com/peterLaurence/MapView. MapView is maintained by Peter, one of o

Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine
Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine

Object Pool Design Pattern with Kotlin Demonstration of Thread Safe Object Pool Design Pattern using Kotlin language and Coroutine. Abstract The objec

Glide Bitmap Pool is a memory management library for reusing the bitmap memory
Glide Bitmap Pool is a memory management library for reusing the bitmap memory

Glide Bitmap Pool About Glide Bitmap Pool Glide Bitmap Pool is a memory management library for reusing the bitmap memory. As it reuses bitmap memory ,

A simple Android utils library to write any type of data into cache files and read them later.

CacheUtilsLibrary This is a simple Android utils library to write any type of data into cache files and then read them later, using Gson to serialize

REST countries sample app that loads information from REST countries API V3 to show an approach to using some of the best practices in Android Development.
REST countries sample app that loads information from REST countries API V3 to show an approach to using some of the best practices in Android Development.

MAJORITY assignment solution in Kotlin via MVVM Repository Pattern. REST countries sample app that loads information from REST countries API V3 to sho

Timber + Logger Integration. Make Logcat Prettier, show thread information and more.
Timber + Logger Integration. Make Logcat Prettier, show thread information and more.

Pretty Timber Android Logcat Timber + Logger Integration Video Instructions: https://youtu.be/zoS_i8VshCk Code App.kt class App : Application() {

Multi-thread ZX0 data compressor in Kotlin

ZX0-Kotlin ZX0-Kotlin is a multi-thread implementation of the ZX0 data compressor in Kotlin. Requirements To run this compressor, you must have instal

Kreds - a thread-safe, idiomatic, coroutine based Redis client written in 100% Kotlin

Kreds Kreds is a thread-safe, idiomatic, coroutine based Redis client written in 100% Kotlin. Why Kreds? Kreds is designed to be EASY to use. Kreds ha

New Relic Kotlin Instrumentation for Kotlin Coroutine. It successfully handles thread changes in suspend states.

new-relic-kotlin-coroutine New Relic Kotlin Instrumentation for Kotlin Coroutine. It successfully handles thread changes in suspend states. Usage 1- U

AudioPlayerView is an Android view that loads audio from an url and have basic playback tools.
AudioPlayerView is an Android view that loads audio from an url and have basic playback tools.

AudioPlayerView AudioPlayerView is an Android view that loads audio from an url and have basic playback tools. It makes use of the Android MediaPlayer

Comments
  • Merge concurrent http request to same url into single req. Use native animation.

    Merge concurrent http request to same url into single req. Use native animation.

    Hi. Thanks for a great library! I wrote a couple of fixes. Please merge if you find these commits useful.

    1. Multiple concurrent requests to same URL were fired if the same image was shown multiple times on the screen.
    2. NineOldAndroids has a memory leak. I don't remember the details anymore. It was a few months ago when I traced the problem. Anyway, let's use native animation if it is available. And anyway, maybe it's not even worth including a big library for such a minor fade in animation.
    opened by tuner 0
  • Being able to specify the bitmap max width and height

    Being able to specify the bitmap max width and height

    If the original image has a large resolution, AndroidImageLoader creates a Bitmap of the same size, even if we need only a small image for a thumbnail. This leads to OutOfMemoryException.

    It would be great if we could specify the max size of the image we want so that AndroidImageLoader could scale down the downloaded image before loading it on disk or in memory : http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    enhancement 
    opened by anthonyfoulfoin 1
  • putBitmapOnDisk function, the process of java.lang.OutOfMemoryError is not.

    putBitmapOnDisk function, the process of java.lang.OutOfMemoryError is not.

    Crash issue putBitmapOnDisk function, the process of java.lang.OutOfMemoryError is not.

    02-21 15:51:15.660: I/dalvikvm(13407): "LifoAsyncTask #3" prio=5 tid=17 RUNNABLE 02-21 15:51:15.660: I/dalvikvm(13407): | group="main" sCount=0 dsCount=0 obj=0x436f5790 self=0x5e329dd0 02-21 15:51:15.660: I/dalvikvm(13407): | sysTid=13660 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1580375192 02-21 15:51:15.660: I/dalvikvm(13407): | schedstat=( 47014896620 14742726206 45724 ) utm=2156 stm=2545 core=2 02-21 15:51:15.660: I/dalvikvm(13407): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~91) 02-21 15:51:15.660: I/dalvikvm(13407): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201) 02-21 15:51:15.660: I/dalvikvm(13407): at android.graphics.Bitmap.nativeCompress(Native Method) 02-21 15:51:15.660: I/dalvikvm(13407): at android.graphics.Bitmap.compress(Bitmap.java:772) 02-21 15:51:15.660: I/dalvikvm(13407): at com.wuman.androidimageloader.ImageLoader$BitmapConverter.toStream(ImageLoader.java:942) 02-21 15:51:15.660: I/dalvikvm(13407): at com.wuman.androidimageloader.ImageLoader.putBitmapOnDisk(ImageLoader.java:673) 02-21 15:51:15.660: I/dalvikvm(13407): at com.wuman.androidimageloader.ImageLoader.access$1300(ImageLoader.java:61) 02-21 15:51:15.660: I/dalvikvm(13407): at com.wuman.androidimageloader.ImageLoader$ImageRequest.writeBackResult(ImageLoader.java:873) 02-21 15:51:15.660: I/dalvikvm(13407): at com.wuman.androidimageloader.ImageLoader$ImageTask.doInBackground(ImageLoader.java:887) 02-21 15:51:15.660: I/dalvikvm(13407): at com.wuman.androidimageloader.ImageLoader$ImageTask.doInBackground(ImageLoader.java:879) 02-21 15:51:15.660: I/dalvikvm(13407): at com.wuman.androidimageloader.util.LifoAsyncTask$2.call(LifoAsyncTask.java:126) 02-21 15:51:15.660: I/dalvikvm(13407): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 02-21 15:51:15.660: I/dalvikvm(13407): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 02-21 15:51:15.660: I/dalvikvm(13407): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 02-21 15:51:15.660: I/dalvikvm(13407): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 02-21 15:51:15.660: I/dalvikvm(13407): at java.lang.Thread.run(Thread.java:856) 02-21 15:51:15.660: W/System.err(13407): java.lang.OutOfMemoryError 02-21 15:51:15.660: W/System.err(13407): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91) 02-21 15:51:15.660: W/System.err(13407): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201) 02-21 15:51:15.660: W/System.err(13407): at android.graphics.Bitmap.nativeCompress(Native Method) 02-21 15:51:15.660: W/System.err(13407): at android.graphics.Bitmap.compress(Bitmap.java:772) 02-21 15:51:15.660: W/System.err(13407): at com.wuman.androidimageloader.ImageLoader$BitmapConverter.toStream(ImageLoader.java:942) 02-21 15:51:15.660: W/System.err(13407): at com.wuman.androidimageloader.ImageLoader.putBitmapOnDisk(ImageLoader.java:673) 02-21 15:51:15.660: W/System.err(13407): at com.wuman.androidimageloader.ImageLoader.access$1300(ImageLoader.java:61) 02-21 15:51:15.660: W/System.err(13407): at com.wuman.androidimageloader.ImageLoader$ImageRequest.writeBackResult(ImageLoader.java:873) 02-21 15:51:15.660: W/System.err(13407): at com.wuman.androidimageloader.ImageLoader$ImageTask.doInBackground(ImageLoader.java:887) 02-21 15:51:15.660: W/System.err(13407): at com.wuman.androidimageloader.ImageLoader$ImageTask.doInBackground(ImageLoader.java:879) 02-21 15:51:15.660: W/System.err(13407): at com.wuman.androidimageloader.util.LifoAsyncTask$2.call(LifoAsyncTask.java:126) 02-21 15:51:15.660: W/System.err(13407): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 02-21 15:51:15.660: W/System.err(13407): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 02-21 15:51:15.660: W/System.err(13407): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 02-21 15:51:15.660: W/System.err(13407): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 02-21 15:51:15.660: W/System.err(13407): at java.lang.Thread.run(Thread.java:856)

    bug 
    opened by erkasraim 1
🍂 Jetpack Compose image loading library which can fetch and display network images using Glide, Coil, and Fresco.

?? Jetpack Compose image loading library which can fetch and display network images using Glide, Coil, and Fresco.

Jaewoong Eum 1.4k Jan 2, 2023
Load images using Glide

Glide_Demo Load images using Glide Image used private val image = "https://cdn.pixabay.com/photo/2018/05/03/21/49/android-3372580_1280.png" Add Glide

Daniel Kago K 0 Nov 1, 2021
Powerful and flexible library for loading, caching and displaying images on Android.

Universal Image Loader The great ancestor of modern image-loading libraries :) UIL aims to provide a powerful, flexible and highly customizable instru

Sergey Tarasevich 16.8k Jan 8, 2023
An Android library for managing images and the memory they use.

Fresco Fresco is a powerful system for displaying images in Android applications. Fresco takes care of image loading and display, so you don't have to

Facebook 16.9k Jan 8, 2023
Android ImageView that handles animated GIF images

GifImageView Android ImageView that handles Animated GIF images Usage In your build.gradle file: dependencies { compile 'com.felipecsl:gifimageview:

Felipe Lima 1.1k Dec 27, 2022
An Android view for displaying repeated continuous side scrolling images. This can be used to create a parallax animation effect.

Scrolling Image View An Android view for displaying repeated continuous side scrolling images. This can be used to create a parallax animation effect.

Q42 1.8k Dec 27, 2022
Media Picker is an Android Libary that lets you to select multiple images or video

Media Picker Please let me know if your application go to production via this link Media Picker is an Android Libary that lets you to select multiple

Abdullah Alhazmy 264 Nov 10, 2022
Splash - Wanted an app that displays images from Unsplash, well here it is

Splash - Wanted an app that displays images from Unsplash, well here it is

Bamidele Ajewole 2 Apr 26, 2022
🦄 Android Pokedex-AR using ARCore, Sceneform, Hilt, Coroutines, Flow, Jetpack based on MVVM architecture.

Pokedex-AR Pokedex-AR is a small demo application based on AR, modern Android application tech-stacks, and MVVM architecture. This project focuses on

Jaewoong Eum 535 Dec 9, 2022
An image loading and caching library for Android focused on smooth scrolling

Glide | View Glide's documentation | 简体中文文档 | Report an issue with Glide Glide is a fast and efficient open source media management and image loading

Bump Technologies 33.2k Jan 7, 2023