Reputation: 43
Can I use Universal Image Loader lib in a static class? Meaning creating one instance of image loader and modifying it to static.
What is the best method to use this lib in different multiple fragments and classes? How can I improve caching feature?
Upvotes: 1
Views: 497
Reputation: 583
The best way to use Universal Image Loader is to create a single instance when your App starts up and then get that instance throughout the app
Here is the App class
public class App extends Application {
public ImageLoader imageLoader;
public ImageLoader getImageLoader() {
return imageLoader;
}
@Override
public void onCreate() {
super.onCreate();
// UNIVERSAL IMAGE LOADER SETUP
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(true)
.cacheOnDisk(true)
.cacheInMemory(true)
.displayer(new FadeInBitmapDisplayer(300))
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.diskCacheSize(100 * 1024 * 1024)
.build();
this.imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
// END - UNIVERSAL IMAGE LOADER SETUP
}
}
Make sure the application tag in AndroidManifest.xml has android:name=”.App” attribute
To get the instance, in an Activity
ImageLoader imageLoader = ((App)getApplicationContext()).getImageLoader();
outside Activity
ImageLoader imageLoader = ((App)context.getApplicationContext()).getImageLoader();
You can refer this blog.
Upvotes: 2
Reputation: 1530
i recommend you to use Glide a very nice image loader lib, i myself has tried and happy using it.
this is what you need to read and implement for image caching and efficient use of lib
https://futurestud.io/tutorials/glide-getting-started
the official link https://github.com/bumptech/glide
Upvotes: 1
Reputation: 10567
ImageLoader.getInstance(); // Get singleton instance
the above line will provide a singleton. you can call it then perform your loading
as the doc shows there is a sample project which you can download form github. this sample project shows how to use this library properly
Upvotes: 3