Download images and put them into ImageViews - android

Hi and happy new year!
I'm trying to download images from internet and put them into different ImageViews. The ImageViews are dynamically created as the user scrolls. When user arrives to the bottom of the scrollview, I load 10 images more.
The images are loading ok, but when i have a lot of images I get a java.lang.OutOfMemoryError.
I know the problem is that I have a lot of images consuming a lot of memory, so... what's the way to go on my scenario?
Thanks!

Try using a ListView instead of a ScrollView. Then you can use a lazy loading technique like Universal Image Loader. The ListView utilizes view recycling which will be easier on your memory, and you can also cache images using the image loader. This library also has a few options for memory management as well.

You are going to have to keep track of the images you have loaded and start recycling them when they are out of view.
You might find the LruCache a valuable tool. There was a good talk at IO12 "Doing More With Less: Being a Good Android Citizen" that went over lots of memory issues and includes some discussion on how to use the LruCache starting around the 4 minute mark.

Related

Getting large number of images in recyclerView android

in my recent android project I have to get a large number of images from the web and show them in my main activity (something like explore part of the Instagram app). I used a recyclerView with gridLayoutManager of 3 columns and I have to get something like 700 images from URL. When I open the app and comment the getting image part, app work perfectly fine (the app get the information of each tile but doesn't display the images). But when I start setting image bitmaps the app start becoming laggy and crash after about a hundred images loaded.
What do you recommend me to do ?
And another question: Was using recyclerView a smart idea ?
Thanks for your reply.
Don't load hundreds of images. That requires a few things:
1)Don't store images in memory in the adapter. Store the url/resourceid/whatever of the image so you can load it on demand.
2)Use an LRU cache of images, limited in size and load your images through that.
3)Make sure that however you download images does not spawn too many concurrent requests, and that requests are canceled when no longer needed (when the view it would go into is recycled).
4)I'd suggest downloading the images and writing them to disk, then loading them from disk as needed. This will prevent you from having to keep the entire file in memory to decode it while downloading it, which will reduce your memory usage while downloading.
5)Do not decode the image on the UI thread. Do it on another thread.
6)If you don't need to display images fullsize, make thumbnails.
Images in a RecyclerView, especially if being downloaded need a lot of work to do well and handle rapid scrolling.
You should post some code here but seems you are asking for some efficient setup.
First of all, try using some image caching libraries like Glide or
Picasso. It manages and caches your images locally so you don't end up
making multiple requests for the same image.
This solves most of your problem and don't try to load 700 images
altogether and display use lazy loading means load first 10-20 images
first and when user scrolls make another API call for another 10-20
and so on.
Here is an article on how to use Glide and how it works.
https://futurestud.io/tutorials/glide-image-resizing-scaling

Jittery scrolling when loading local images in recyclerView using Picasso

I am loading images in a recyclerView using picasso in my app. The image files are all local files. The problem is that the listview scrolling is not smooth, it is jittery. I searched about it and I saw that a lot of people have had similar problems but it is really strange that there is no clear solution available.
Horrible performance when loading local files
Picasso is awesome, but for loading local device images as thumbnails into a gridview (for example), Picasso is slower ...
Recyclerview painfully slow to load cached images form Picasso
From the above links it seems that picasso works great when fetching images from the web but for locally stored images it doesn't do that great.
Is it even recommended to use Picasso in this case? Should I have my own implementation of LruCache and remove Picasso? I have done it without Picasso using caching myself using LruCache. Though the scrolling is flawless in that case but Picasso is much more clean and compact, so I thought it might be the better solution in the long term.
There could a lot of reasons for this kind of behaviour. One issue could be that your row layout may be very deep rather than being wide. Another issue could be the size of images, if images are of large size, there is a good chance that all of them might not fit into Picasso's cache.
If the problem is due to the size of images, you can try Fresco by Facebook. It is very good at loading large images. It uses native as well as ashmem cache, so it can hold large amounts of images in cache compared to other similar libraries like Picasso or Glide. Another thing you can do is, android:largeHeap="true" in your AndroidManifest.xml inside the Application tag.

Best Way to Animate images frame by frame in Android

I have 3 sets of 50 images and I have to create the animations for each set of images in Android application. I am able to create a simple application which animate first set of 50 images using the below method,
Added Animation-list xml in drawable folder and called it using frameAnimation.start().
This method didn't work until I kept the following "android:largeHeap="true" in manifest file.
I am not sure whether this is the good way to animate the images (if we have more number of images and each image of more size like 60 KB. Image is JPG format) or not
I browsed and I found that, if we are able to clear the memory and if we are able to maintain less number of images in memory, then our application will work very fine. So I want to know how to clear the memory?
Please let me know do I need to follow different method to animate the images other than I explained above, so that I can clear the memory.
Thanks for your help.
After looking into several posts and doing much research, I finally ended up modifying the images in Imageview programmatically to simulate frame animation. That way I am just maintaining three images in cache at any point of time (previous, current and next one). I hope that this will be useful to others.
It is not a good thing to process a large amount of images in a frame like manner in Android itself as it will trigger the dreaded "Out of Memory" exception.
Clearing the memory itself is not possible. The best fix for this is proper bitmap handling of the app.
I'm not sure but you might want to check on PhoneGap.
Its an HTML5 Engine that we used before to create a game.
By drawing into the canvas itself, we've recreated frame animation with it. It just requires WebDev skills though.

Issue in displaying images

Description:
I am working on an application which includes displaying of images present on server. I am using grid view to display the images. Now as the images are in large amount. I am confuse whether first i should save the images on sqlite.
How can i improve the performance of the application.
You can cache the images within the client side for better performance.
These links may help you, lets have a look..
Multithreading For Performance
Android Imagedownloader project on Google Code

Out of Memory with ListView

I am very familiar with OOM concept. I have come across so many questions that have been asked here and I know this could sound like a repeated question on OOM, but I have a different problem now.
I have hundreds and hundreds of images, which I have to show in a single ListView in a single Activity.
I have made use of the Fedor's lazy loading concept.
The code works fine when the images are little(something like 50-100 images), but if I get around 200 to 300 images, as soon as the user makes a scroll through the ListView (eventually) I am getting an out of memory exception.
I am using the efficient Adapter concept as shown here in developer site and I believe that this is the best custom adapter usage method.
All the suggestion what I have seen so far is based on reducing the bitmap size by scaling it or some other means. I have already tried those methods. But still the OOM occurs in some point of time.
Reducing the quality of the image doesn't look like the way to handle this Exception , because which seems to affect user experience and, also, it doesn't work for a large amount of bitmaps.
My questions here are:
How do you display n images in a single ListView without facing OOM?
What are the best strategies to follow in this scenario?
Is it possible to show around 500 images in a single ListView?
Any help is much appreciated.
Look into googles shelvesproject.
Shelvesproject does following: uses a placeholder image while loading image, use weakreference for storring images, delayed handler updated with clearing.
Yes and no, it is possible to have listview with this many items but its not possible to have this many images all in memory at the same time.
Try with API Level 11+ images before this API Level are placed in application heap. And yes it is possible to have a List View with a 500 images and working smooth (Contacts application for example).
This http://code.google.com/p/android-imagedownloader/ may help you getting images, cache and recycle them.

Categories

Resources