How to cache image with universal image loader in android - android

I use UniversalImageLoader library for android:
I would like to cache image but now it is downloading everytime:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.build();
ImageLoader.getInstance().displayImage(url, iv, options);
How should i configure the options to cache the images to disk?

Try both of this code :
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisc(true)
.build();
I used both of them and the images are cached.

Related

Cache duplicate Universal ImageLoader and Picasso

Use two biblotecas in my application, Universal Image Loader and another that uses the library picasso,
the picasso library, records the image in the cache /cache/picasso-cache/ and the names are generated with the MD5 URL.
To separate the cache Universal Image Loader, I write to /cache/LazyLoad/ and names generated with the item ID.
My code of configuration Universal Image Loader
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(true)
.cacheOnDisk(true)
.cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300))
.build();
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(getApplicationContext());
config.defaultDisplayImageOptions(defaultOptions);
config.memoryCache(new WeakMemoryCache());
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // for debug
FileNameGenerator fileNameGenerator = new FileNameGenerator() {
#Override
public String generate(String imageUri) {
String fileName = imageUri.substring(imageUri.lastIndexOf('/')+1, imageUri.length());
String fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.'));
return fileNameWithoutExtension;
}
};
String pathCache = Globais.getCacheDirImageLazyLoad(mContext); //returns StorageUtils.getCacheDirectory(context, false).getAbsolutePath() + "/lazyload";
File f = new File(pathCache);
if (!f.exists())
f.mkdirs();
DiskCache diskCache = new UnlimitedDiskCache(f, null, fileNameGenerator);
config.diskCache(diskCache);
config.diskCacheFileNameGenerator(fileNameGenerator);
ImageLoader.getInstance().init(config.build());
Problem that after I started using the component that uses the picasso, began to double the cache, ALL images that use the Universal Image Loader, also appear in Picasso's cache.
How can I fix this ???
Anyone have any idea?

java.lang.IllegalStateException: ImageLoader must be init with configuration before using

My app uses Universal ImageLoader and is on production now.
I installed bugsense(MINT) and reported this error.
I have already initialised ImageLoader in application class but somehow application instance is dead and activity is still trying to use this library onResume.
any help would be appreciated.
Here is initalize code in application class.
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.writeDebugLogs()
.defaultDisplayImageOptions(defaultOptions)
.diskCacheExtraOptions(480, 320, null)
.build();
ImageLoader.getInstance().init(config);
Can you please try init using ImageLoaderConfiguration
protected ImageLoader imageLoader;
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(this));

Images not cache in memory with Univeral Image Loader

I am using Universal image loader to load images from the url. but images are not getting stored in the cache after loading. can anyone help? this is the code
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.no_pic)
.showImageForEmptyUri(R.drawable.no_pic)
.showImageOnFail(R.drawable.no_pic).cacheInMemory(true)
.considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
c).threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.tasksProcessingOrder(QueueProcessingType.LIFO)
.writeDebugLogs().build();
// if (!imageLoader.isInited()) {
imageLoader.init(config);
use cache on disk instead of cache on memory

android universal image loader not caching or saving image

I am using universal image loader to load images. The issue is that It does not save loaded images in cache Here is my code.
Map<String, String> headers = new HashMap<String, String>();
headers.put("key", Commons.CURRENT_ACTIVE_PROFILE.getKey());
headers.put("secret", Commons.CURRENT_ACTIVE_PROFILE.getSecret());
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mActivity)
.imageDownloader(new CustomImageDownaloder(mActivity)).build();
//imageLoader.init(ImageLoaderConfiguration.createDefault(mActivity));
imageLoader.init(config);
imageLoader = ImageLoader.getInstance();
displayImageOptions = new DisplayImageOptions.Builder()
.extraForDownloader(headers)
.showImageForEmptyUri(R.drawable.no_preview)
.showImageOnLoading(R.drawable.no_preview)
.showImageOnFail(R.drawable.no_preview)
.cacheInMemory(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
The problem is when I use following line for ImageLoaderCinfiguration that creates a default settings It would work fine and saves the images in cache.
imageLoader.init(ImageLoaderConfiguration.createDefault(mActivity));
But I want to use custom settings for ImageLoaderConfiguration because I am using CustomImageDownloader to pass key/secret with URL. So i use following lines for config
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mActivity)
.imageDownloader(new CustomImageDownaloder(mActivity)).build();
But In this case it doest not save image and if I run the app without internet it does not load the images. Any help/suggestion please?
Possible duplicate I have tried that but no use.
U can use this simple method for Image Loader view
private void initImageLoader() {
// ImageLoader initializing
DisplayImageOptions opts = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(opts).build();
ImageLoader.getInstance().init(config);
}
I have resolved the issue myself. as using
.imageDownloader(new CustomImageDownaloder(mActivity)).build();
would override the default imageDownloader that is
.imageDownloader(new BaseImageDownloader(getApplicationContext())) // default
Default image downloader class contains diffrent methods to load images from web, local storage, assets etc. But the CustomImageDownaloder that i have created only contains code to get stream from web/network. So what I did is to copy and paste all the code of default BaseImageDownloader to CustomImageDownaloder and just modify the
getStreamFromNetwork(String imageUri, Object extra)
according to my requirements.

Get Android Universal Image Loader cache/save images with file extension

I am using the Android-Universal-Image-Loader from https://github.com/nostra13/Android-Universal-Image-Loader to load and cache images async. The problem I am facing is that i would like to open the images with the gallery or sent them to friends. For some reason my images are cached without a file extension.
I am using the current configuration
public static void initImageLoader(Context context) {
File cacheDir = StorageUtils.getOwnCacheDirectory(context, CommonUtilities.APP_DIR);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.MAX_PRIORITY)
.discCacheExtraOptions(480, 800, CompressFormat.PNG, 75, null)
.denyCacheImageMultipleSizesInMemory()
.discCache(new UnlimitedDiscCache(cacheDir))
.defaultDisplayImageOptions(options)
.tasksProcessingOrder(QueueProcessingType.FIFO)
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
}
Hopefully anyone can help me out!
Create own FileNameGenerator which will generate file names with extension in the end and then set it into disc cache:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
...
.discCache(new UnlimitedDiscCache(cacheDir, myGenerator))
...
.build();
Create custom file name generator
import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;
import static java.lang.String.valueOf;
public class HashCodeFileNameWithDummyExtGenerator implements FileNameGenerator {
#Override
public String generate(String imageUri) {
return valueOf(imageUri.hashCode()) + ".png";
}
}
Use it
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
...
.diskCache(new UnlimitedDiskCache(cacheDir, null, new HashCodeFileNameWithDummyExtGenerator()))
...
.build()

Categories

Resources