Android gallery with bigs images for zoom - android

I try to implement a Gallery where each item is a big image for a good zomm-in function.
The problem is the management of the memory.
When i try to go to the fourth element android goes to Out Of Memory, so i try to recycle the images not displayed, but android goes to RunTimeException for try to use a recycled image when i come back.
Also i try to change the bitmap of the bigs images not displayed with bitmap of small images but always goes to Out Of Memory.
How can i do, for manage the memory of the Gallery?

After you do call recycle(), make sure you don't use that Bitmap reference again as that will cause a runtime exception if certain methods are invoked. Try using SoftReferences wrapped around your bitmaps. This will guarantee that all of the memory that is no longer references will be reclaimed before an OutOfMemoryError is thrown.

Related

memory not reclaimed after removing cardview on swipe

I am using itemTouchHelper to remove a cardView from a RecyclerView on swipe
my cardview is simple it just has a text and an image
but when i swipe to remove the card disappears but is never taken out of memory (i am saying this because the memory of the image never goes away and it causes an out of memory exception)
my code is very simple so i was wondering if there was a step i was messing that tells the recycler view to remove from memory?
my code is exactly like this one
Usually, images are loaded in android memory, scaled according to device screen resolution requirements, and kept there till the process dies or, is killed or, is collected by garbage collector.
It is possible that your image size in card view (or other images in your res folder) is/are large, and images after scaling by android is occupying too much of memory, to cause memory exception. Try reducing size of your image (Try compare size with the icon sizes that google icons has, they all are less than a Kb, amazing !).
If the above does not work, then create a drawable-nodpi folder in your res folder and put large size images in this folder, this will indicate android to not to scale these images and render it as it is. (make sure you test in all screen devices after following this approach, because now the image won't scale in different screen sizes).
Finally, if none of the above helps try running : System.gc() after there is a card swipe action performed by user. This will run garbage collector and your image will be removed from memory. (This approach does not guarantee garbage collector working in all devices, so make sure that you try for above two approaches, they should work).
Hope this helps !

What happens to changed Images

In android app, images can be changed through run-time (e.g. layout-bg, ImageButton-src, Button-bg, ..etc), so what happens to the old image in the terms of memory occupation? is it removed or still occupy a portion from the memory? and if it's not removed, how can I clear the memory from this image?
UI part of android is mainly written in Java -- once all references to a bitmap is gone, the bitmap will be flagged for garbage collection. If you do not use a bitmap texture, and you have no reference to that bitmap anywhere in the code the memory will be freed immediately. Please be careful about memory leak in android. Read this:
http://android-developers.blogspot.ca/2009/01/avoiding-memory-leaks.html

Android how to load many image to ListView without getting Out Of Memory?

I have a ListView, each row have an ImageView to load a small icon. When I scroll down the list, new rows will be load dynamically. But when I scroll up, the old rows just show up which mean all the image in ImageView are cache in the memory right? Now if I scroll down long enough, the memory to store bitmap image will be full and I get Out Of Memory Error. So what's the best way to load Image to ListView in this situation?
EDIT: Is there a way for me to clear all bitmap image in memory when my activity finish?
Use below two link for that, it may help you.
Lazy List
Universal ImageLoader
When you are done with showing or setting bitmaps,
use bitmap.recycle() and bitmap = null
this will help to Garbage Collector to clean memory.

Android RuntimeException vs. OutOfMemoryError

I have developed a PhotoViewer application wihch simply lists the image files in external storages and shows them if the user wants to see one of them. My problem occurs when my thread drawing the thumbnails for the images.
If the count of images is less than about 500 it is ok for me and there is no issue but if the user has over 1000 images (it is quite possible) afters some drawing thumbs the memory usage exceeds the limit and I get OutOfMemory Exception.
I have tried to use bitmap.recycle() but this time I have faced with using a recycled bitmap error (RuntimeException). Could you suggest me a way to show more and more thumbnails (over 2000 maybe) for the images in external storages?
Your OutOfMemory problem will probably be resolved if you got recycle() working correctly. The RunTimeException will be an ImageView still trying to draw a bitmap which has been recycle, so you must ensure that nothing is using the bitmap before you recycle it.
You could also stop thumbnails being loaded whilst the ListView is being scrolled using a ListView.OnScrollListener - without this, if the user flings through the list, all intermediate rows that scroll past are still getting loaded.

When is it safe/possible to recycle a bitmap in Android SimpleCursorAdapter with Gallery view?

When displaying a gallery with lots of large bitmaps (fullscreen, wvga), I quite frequently get out of memory issues. I assume this is related to bitmaps not being recycled. When/how can I force bitmaps to be recycled?
I also noticed that in the getView method of simpleCursorAdapter, convertView is always null. I assume this means that the old view is not recycled? Even when scrolling back and forth, a new view is created each time. However, scrolling back and forth does not cause out of memory issues, that only happens when the total number of images is large enough.
When using the same adapter with a ListView, views are recycled, so it seems the problem is with the Gallery.
What can I do to force views and/or bitmaps to be recycled? What else can I do manage memory without reducing gallery size and bitmap quality.
You can use recycle() if you are sure tou won't use a bitmap anymore, for example in any operation with a temporary bitmap. But...You can use BitmapFactory.Options, for ex:
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inPurgeable=true;
Bitmap bitmapTemp = BitmapFactory.decodeResource(getResources(), R.drawable.intro, bitmapOptions);
What it does? the option inPurgeable will asume that once the system needs memory and your bitmap it's no more useful, system will recycle this memory allocation by itself. If for some reason you cannot load an image with the BitmapFactory then you can use Recycle() and it's recommendable to call de garbage collector then with system.gc(), this is usefull if you're developing for example a game, because when garbage collector it's activated when you system it's low on memory, it will consume some valious milliseconds.
You should be very careful with bitmaps, they're not stored on the "normal" memory used by your app, it's stored on a "general" memory used by all your apps, unless you are using Android 3.0.
If you get a memory leak and can't find it looking at the heap... you should take a look at your code loading and using bitmaps.
Hope it helps.
You never need to recyle a Bitmap. However, you can do it, and it will help when you have OutOfMemory errors. But don't it until the bitmap is not needed any more, because (from the javadoc)
it will throw an exception if
getPixels() or setPixels() is
called, and will draw nothing. This
operation cannot be reversed, so it
should only be called if you are sure
there are no further uses for the
bitmap.
In your case, I assume you should:
use thumbs which are bitmap opened with BitmapFactory.Options.inSampleSize or MediaStore.Images.Thumbnails
Have only at most one full size bitmap in memory (maybe one preloaded in you can afford)

Categories

Resources