I'm loading about 50 images. In my activity I have this configuration
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisk(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.resetViewBeforeLoading(true)
.displayer(new FadeInBitmapDisplayer(300))
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.diskCacheSize(100 * 1024 * 1024)
.build();
ImageLoader.getInstance().init(config);
in binderdata
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(uri, holder.iv_img);
Some images are loaded, but some are not and I get OutOfMemory error.
Check this tips from Useful Info
If you often got OutOfMemoryError in your app using Universal Image
Loader then:
Disable caching in memory. If OOM is still occurs then it seems your app has a memory leak. Use MemoryAnalyzer to detect it. Otherwise
try the following steps (all of them or several):
Reduce thread pool size in configuration (.threadPoolSize(...)). 1 - 5 is recommended.
Use .bitmapConfig(Bitmap.Config.RGB_565) in display options. Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888.
Use .imageScaleType(ImageScaleType.EXACTLY) (Your already use this tips)
Use .diskCacheExtraOptions(480, 320, null) in configuration
Hope this helps!!
1.I would suggest you to checkout Image Management Library by Facebook that is Fresco that is pretty awesome and mature as compared to other Image Loading Library.
2.Fresco handles all the things caching of images with 3 Tier architecture ( BITMAP_MEMORY_CACHE, ENCODED_MEMORY_CACHE and DISK_CACHE). It also reduces OOM(Out Of Memory) issues. When image in a view goes out of screen it automatically recycles the bitmap, hence releasing the memory.
3.Fresco has SimpleDraweeView as custom image view which supports Rounded Corners and Circles link and supports Animated(.gif, .webp) as well as Normal Images(.jpg, .png).
Related
I started to use Universal Image Loader instead of Picasso, to avoid the image-not-loading problem. Now it emerges in Universal Image Loader too. It is the same problem with the same unique image, but not other images.
The problematic Image:
http://luxproperty.kaytami.com/platform/media/image/jpeg/2015/01/4_4.jpg
Other properly-behaved images:
http://luxproperty.kaytami.com/platform/media/image/jpeg/2015/01/4.2_4.jpg
http://luxproperty.kaytami.com/platform/media/image/jpeg/2015/01/4.3_4.jpg
http://luxproperty.kaytami.com/platform/media/image/jpeg/2015/01/4.4_4.jpg
They are of similar sizes. And about my Universal Image Loader, the setting is as such:
In Application:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.diskCacheSize(50 * 1024 * 1024) // 50 Mb
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
In activity:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.image_placeholder)
.showImageForEmptyUri(R.drawable.image_error)
.showImageOnFail(R.drawable.image_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.build();
.......
ImageLoader.getInstance().displayImage(url), image, options);
Any hints would be much appreciated. I just cannot get rid of this problem anyhow, neither with Picasso nor Universal Image Loader.....
My previous StackOverFlow with Picasso:
Picasso cannot load images for some URL (no special characters)
It seems like the image is corrupted as downloading and opening it in photoshop also throws error. You can also see the bottom part of the image for blurry pixels.
Although there is one more case that I know of when image loading fails is when loading an image in CMYK color format. Android doesn't support CMYK, although there are some Third-party libraries for loading CMYK images but I don't think that it is the case for you. I strongly believe that the image is corrupted.
imageLoader = ImageLoader.getInstance();
prova = context;
options = new DisplayImageOptions.Builder()
//.displayer(new RoundedBitmapDisplayer(1000))
.showImageOnLoading(R.drawable.placeholder)
.showImageForEmptyUri(R.drawable.placeholder)
.showImageOnFail(R.drawable.placeholder)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
// .displayer(new RoundedBitmapDisplayer(70))//////
.build();
//note you use this in your adapter where your handling your image and make sure you inistalize that your UIL class in your manifest application
<application
android:name="com.example.UILApplication"
I am using Universal Image Library (UIL) to load images in my application.
Now problem is that I can have different images on same URL. I mean today I have a cat picture in a URL and tomorrow I might have a dog picture on same URL. So I want to check that if cached image is 2 days old then delete it and download the image again.
This is my configuration.
File cacheDir = StorageUtils.getOwnCacheDirectory(this, "FriendListCache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
// You can pass your own memory cache implementation
.diskCache(new UnlimitedDiscCache(cacheDir))
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.build();
ImageLoader.getInstance().init(config);
And this is image loading options.
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.medium_load)
.cacheInMemory(true)
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
.decodingOptions(resizeOptions)
.displayer(new RoundedBitmapDisplayer(160))
.build();
UIL provide LimitedAgeDiscCache option for your requirement.
LimitedAgeDiscCache (Size-unlimited cache with limited files' lifetime. If age of cached file exceeds defined limit then it will be deleted from cache.)
.discCache(new LimitedAgeDiscCache(cacheDir, 14400))
I'm using the "Android universal image loader library (v 1.9.1)" and I'm trying to limit the amount of (disc) cache being used by using this line in a config builder:
.discCache(new TotalSizeLimitedDiscCache(cacheDir, 25 * 1024 * 1024))
I might be doing something really stupid here, but this was supposed to limit my cache to 25MB and when I look at the current App info I'm already at 45+ MB, so it doesn't seem to be working.
Have I missed something?
added suspicion: what if the cache-number also includes the 'memorycache' that is set to true? I working under the assumption this is only a cache as far as the imageLoader is concerned, and from an application/os point of view just another thing using memory. if it isn't, it might explain the numbers?
Complete code for initializing :
private void initImageLoader() {
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisc(true)
.build();
File cacheDir = StorageUtils.getCacheDirectory(getApplicationContext());
cacheDir.mkdirs();
ImageLoaderConfiguration config = new
ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.discCache(new TotalSizeLimitedDiscCache(cacheDir, 25 * 1024 * 1024))
.build();
ImageLoader.getInstance().init(config);
}
And for calling the code (showing imageUrl in imageView)
ImageLoader.getInstance().displayImage(imageUrl, imageView);
When setting the default memory-cache to false (so don't use .cacheInMemory(true)), the cache seems to be keeping itself to the set maximum. The computed cache is probably a combination of the on-disc cache and the memory cache.
This does make the "cache" number from the Android OS somewhat less usable, but on the other hand, it is kinda fair. As it looks like the numbers are lineing up, it's just a matter of setting both maximums, and communicating this to the user.
I'm using the Universal Image Loader 1.8.6 library for loading dinamically images taken from web.
The ImageLoaderConfiguration configuration is the following:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.discCacheSize(50 * 1024 * 1024)
.discCacheFileCount(100)
.imageDownloader(new BaseImageDownloader(getApplicationContext())) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build();
and the DisplayImageOptions configuration is:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.no_foto)
.showImageForEmptyUri(R.drawable.no_foto)
.showImageOnFail(R.drawable.no_foto)
.build();
The method getView inside my ArrayAdapter contains the code:
iv.setImageResource(R.drawable.no_foto);
imageLoader.displayImage(basePath+immagine, iv);
The first line of the above code is used just for avoid that the recycling of view in gridView allow to set image in the wrong position (until the picture has not been downloaded).
The actual problem is this:
If I open my Activity (containing the GridView) the shown items thanks to the UIL library can download the image. (Meanwhile the no_foto.png image is shown in each view of the grid). When all the views have loaded their own images, if I try to scroll down and then I come back (scrolling up) I have to wait that the images are reloaded, because all the views are now occupied by the no_foto.png image.
How can I avoid the reload of these images? Using Universal Image Loader, can I cache the images previous loaded?
NOTE: Since my grid can contain many images I would to use an implementation of disk cache (not memory cache).
I found that .discCacheFileCount() can be used for setting the maximum number of cached files, so why it seems that my files are not cached?
I have solved the problem
I was just declaring option, but I wans't using it, so I have modified the line:
imageLoader.displayImage(basePath+immagine, iv);
into:
imageLoader.displayImage(basePath+immagine, iv, options);
and I have added in the options the method:
.cacheOnDisc(true)
Caching is not enabled by default in UIL, so if you want use the cache you should use
// Create default options which will be used for every
// displayImage(...) call if no options will be passed to this method
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
...
.cacheInMemory()
.cacheOnDisc()
...
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
...
.defaultDisplayImageOptions(defaultOptions)
...
.build();
ImageLoader.getInstance().init(config); // Do it on Application start
And while loading image use:
// Then later, when you want to display image
ImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used
Another way is
DisplayImageOptions options = new DisplayImageOptions.Builder()
...
.cacheInMemory()
.cacheOnDisc()
...
.build();
ImageLoader.getInstance().displayImage(imageUrl, imageView, options);
And you can find more information here
I have a problem using the Universal Image Loader Library.
Unfortunately, the Library causes huge memory usage of my application.
I have a ScrollView (there are tons of reasons why I am using a ScrollView instead of a ListView, most of them have to do with custom insertion and item selection animations) holding about 20 custom Views.
Each of those custom views consists of some TextViews as well as an ImageView.
And now here is the thing:
The Images dispalyed in the ImageViews are downloaded from the Internet and all have around 100-150kb (dimension 640 x 320) each. If I download the Images using the UniversalImageLoader my app crashes with an OutOfMemoryException because it uses around 80Mb of memory.
If I do not download the Images, and just put in a Hard-Coded Image with the size of 1200kb (dimension 1920 x 1080) per custom view, my app only consumes around 40Mb of memory.
What am I doing wrong using the UniversalImageLoader when downloading ten times smaller Images causes my app to use up twice as much memory?
I disabled all caching mechanisms but the problem still persists.
Here is my UniversalImageLoader setup:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(c)
.build();
ImageLoader.getInstance().init(config);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.loading)
.showImageForEmptyUri(R.drawable.loading)
.showImageOnFail(R.drawable.loading)
.cacheInMemory(false)
.cacheOnDisc(false)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
.build();
And in code, I call it like this:
ImageLoader im = ImageLoader.getInstance();
im.displayImage("myurl", myimageview, options);
What am I doing wrong? Why is the UniversalImageLoader using up so much memory?
For all the other people out there having the same issues:
I managed to minimize my Memory usage and get rid of the OutOfMemoryException by following these guidelines:
https://github.com/nostra13/Android-Universal-Image-Loader#useful-info
The most important part for me was to reduce the ThreadPoolSize down to 2. I could not feel any performance changes after setting it down.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(c)
.threadPoolSize(2)
.build();
DisplayImageOptions options = new DisplayImageOptions.Builder()
// other options
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.EXACTLY)
// other options
.build();
This reduced my applications total memory consumption by about 35%.