Skip to content

Latest commit

 

History

History
160 lines (107 loc) · 5.3 KB

README.textile

File metadata and controls

160 lines (107 loc) · 5.3 KB

ImageLoader

Image loader is a simple library that give you the ability to decouple the logic to fetch, load and cache images.
It does that away from the ui thread, and by caching images with two levels in memory or in the sdcard if awailable.

How to use the library

Using maven

If you are using maven you need to define the repo and then the dependency

<repositories>
  <repository>
    <id>public-mvn-repo-releases</id>
    <url>https://github.com/novoda/public-mvn-repo/raw/master/releases</url>
  </repository>
</repositories>
<dependency>
  <groupId>com.novoda.imageloader.core</groupId>
  <artifactId>imageloader-core</artifactId>
 <version>1.4.6</version>
</dependency>

Direct jar usage

If you don’t care about building tool you can just download the jar manually from this link

https://github.com/novoda/public-mvn-repo/raw/master/releases/com/novoda/imageloader/imageloader-core/1.4.6/imageloader-core-1.4.6.jar

How to implement

The demo project is a good way to start. Import it in eclipse and check the task list to see where are all the TODO list.
If you are lazy this are the steps :

Application

In the application add the folling code the instantiate the image loader:

@Override
public void onCreate() {
    super.onCreate();
    LoaderSettings settings = new SettingsBuilder()
      .withDisconnectOnEveryCall(true).build(this);
    imageManager = new ImageManager(this, settings);
}

public static final ImageManager getImageManager() {
    return imageManager;
}

This is setting up the image loader with size of the images that are taken from the display and the background image use while the image loader is fetching the real image.

The normal image loader works with softreferences. With a system with low memory as android, the system reclaim space to often. This limit the performance of the cache.

To avoid that we added a LruCache in the image loader library. This is very useful when you have lots of small images.

private static final int NUMBER_OF_IMAGE_IN_CACHE = 50;
...
settings = new SettingsBuilder()
  .withCacheManager(new LruBitmapCache(NUMBER_OF_IMAGE_IN_CACHE)).build(this);
thumbnailImageLoader = new ImageManager(this, settings);

Additional settings

Image loader use UrlConnection to fetch images. There are two important parameters that UrlConnection uses connectionTimeout and readTimeout.

SettingsBuilder builder = new SettingsBuilder();
Settings settings = builder.withConnectionTimeout(20000)
  .withReadTimeout(30000).build(this);

The connection timeout is the timeout in making the initial connection. The read timeout is the timeout on waiting to read data.

Cleaning up sdcard

If you want the image loader to clean up the sdcard, you can use the following line of code on the create of the application:

imageManager.getFileManager().clean(this);

In the settings you can override the default expiration period that is set to 7 days.

In the activity

When you need to load the image in a view object you just need to get the imageLoader from the application and call load.
This is an example of a list view with the binder setting the url in the imageView as a tag.

ImageTagFactory imageTagFactory = new ImageTagFactory(this, R.drawable.bg_img_loading);
imageTagFactory.setErrorImageId(R.drawable.bg_img_notfound);
...

private ViewBinder getViewBinder() {
  return new ViewBinder() {
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
      // TODO add this to your class
      ImageTag tag = imageTagFactory.build(cursor.getString(columnIndex))
      ((ImageView) view).setTag(tag);
      imageLoader.load(view);
      //
      return true;
    }
  };
}

The image loader will fetch the image from the cache if is still there or from the sdcard or will fetch the resource from the network in the worse scenario.

In the manifest

There are two things you need to add : Permissions and the service to clean up the files.

<uses-permission a:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission a:name="android.permission.INTERNET" />
<uses-permission a:name="android.permission.ACCESS_NETWORK_STATE" />

<service android:name="com.novoda.imageloader.core.service.CacheCleaner" android:exported="true">
  <intent-filter>
    <action android:name="com.novoda.imageloader.core.action.CLEAN_CACHE" />
  </intent-filter>
</service>

Helping out with the project

Report issues

If you have any problem with the library or new functionality you need to add, let us know and we will do something about it.
Send us a message or create an issue with github.

ImageLoader projects

  • core contains a java maven based project
  • demo mvn android based project to test imageLoader
  • acceptance mvn android based project with robotium test
  • monkey some example that take a snapshot using monkey runner (deprecated)

To build the projects :

  • run : mvn clean install -Peclipse

Parent project and core

Import the projects with maven, should be easy and painless.

For demo and acceptance

  • create a new android project in eclipse
  • add the necessary jars from the libs folder

Requirements

  • maven 3.0.3+