Home

Awesome

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:

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

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:

SAMPLES

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

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