I know this question is asked several times, but still clarity to my situation would be helpful.
I am showing some images in 2 column grid view. When user taps on an image, I am displaying the image in ViewPager. The Image which is displayed in gridview is about 200X200px and I want to show the same image with enlarged size say about 800X800px in ViewPager in a DialogFragment.
The Actual size of images are huge with different resolutions. I have followed the link http://developer.android.com/training/displaying-bitmaps/index.html and I have scaled down images to size which is required for my app. In ViewPager fragment dialog I am also recycling the image with bitmap.recycle() and calling System.gc() explicitly(I know this is a bad practice) at PagerFragment onDestroy. But even then I am getting Out of memory error. On top of it, I am encountering this issue only in Android 4.2.1 (Nexus 7) but not on Android 4.1.2( Samsung Tab) and Android 4.4(Nexus 7).
For later 2 android versions, I do not have to call even System.gc(). It works very well without this. I have checked Viewpager with some 300 - 400 images. But for former after scrolling 60 images, App is crashing with OutOfmemory error. To resolve this I have used a workaround for now android:largeHeap="true" which I think is very bad for myapp and could not digest it.
I really appreciate if anyone who can help me avoid android:largeHeap="true".
In my app, I have to show around 6000 images in GridView and also an enlarged image in ViewPager linked to GridView. I am loading only 20 images from FileSystem asynchronously while scrolling gridview.
Thanks in advance...
Use an LRUCache to hold the images. Make that the only thing that holds a Bitmap that's not currently on screen. That way the older bitmaps will be kicked out and garbage collected quickly.
Related
I am getting random out of memory exceptions in my app caused by inflate exceptions.
I have 7 fragment (difficulty) activities which launch their own activities via buttons. Each fragment has a scroll view with 30 buttons (levels).
I have set it up so that i can swipe across to each fragment and the fragment takes up the entire screen.
Occasionally when i swipe a few times and then select a random button from a group of 30 it will crash. It tries to load the activity and gives an out of memory exception with an inflate exception on a random line. The line always falls on an imageView or imageButton in the xml file. The activities that load are a grid of imageViews and imageButtons.
I do not get the exception much but it is something i want to fix. I have looked at many other out of memory exception questions although none have helped me. I have done a Memory analyser test and it shows nothing out of the ordinary.
I believe that the imageViews and imageButtons are using too much memory, although i only ever have one activity open at once.
It IS because of your images that are loading. When you load an image and you move around the page and view another image the heap increases. As you continue the process of viewing random images the heap grows even more until your app crashes. It's like stacking books on a glass table. You either move(cache) a book(image) or the glass(app) breaks. You should use an imageloader to load your images.
https://github.com/nostra13/Android-Universal-Image-Loader
You've mentioned that it always falls on an ImageView and ImageButton - and this is the clue to solve this problem. You get OOM 'cause background resource of this view has high resolution and takes a lot of memory. Try to lower resolution of this image.
Also you've mentioned that you have a ScrollView and this means that you keep in memory every 30 items. Probably you'd better change it to RecyclerView backed by adapter.
Just had the same problem and I'd like to simplify all the things said here:
Simply: reduce your images sizes.
Don't use 1080X1920 images... It's too high res.
Such image, even if compressed, when deployed will catch about 1080X1920X4B = 8.2MB (The GPU has to deploy it to it's full original resolution... That's why compression won't help but reducing the needed memory size...) and this i RAM that we're talking about..
Take Gimp or Photoshop and down scale the image to, say, 1/4: 540X960, and you won't feel the difference.. Belive me, been there already.
Beware of the memory consumption of images and videos.
Hope this helps,
James
I have a huge image to render (1024x25373p) cut into 99 images of 1024x256p.
I have tried to use a ListView, but without success : it crashes when scrolling, whithout any error (exept one line saying the proccess was stopped).
So, my question is, how do I render this huge image ?
Please note that I have tried to use TileView by moagrius, without success (I can't get it to work with navigation drawer)
As a suggestion ,
If this is listview, you may not need such larger size images "1024x256p" .
Actual size of your imageView may much for less than that. So its a wastage of
heap if you try to load those images directly without some processing.
Definitly you need to do some scaling down for your image based on actual size that you need. Nice exapmple and code has been published on the official doc
You need to deallocate memory or clear the all bitmaps which are not visible on the listview for particular moment.
You can use progressing loader in order to load your images in to the listview. Then loading will be happen based on the scrolling.
Also you can define lager heap enable in your manifest though it is not recommended but has to do in highly memory consumable apps.
android:largeHeap="true"
I'm using Android-Universal-Image-Loader in a ListView of mine and I'm trying to find the best solution to following:
using resetViewBeforeLoading is necessary or else I get the same image in my ConvertViews, but this causes jitter, unless..
I use PauseOnScrollListener which is otherwise great, except that it shows a blank in some ConvertViews even for images that are already downloaded (I'm using memory and disk caches), so it's confusing to the user who sees a blank for an image they saw only 2 swipes ago
So it seems that I can't get an instant image load (for already-downloaded images) on scroll without jitter, even for images in memory, is this about right? Is there a better or more standard way to do this? (Vertical list-view showing screen-width images, sort of like the Instagram app, which does it buttery-smooth)
Otherwise, is there a way to lengthen the number of convertViews in my ListView to prevent unnecessarily aggressive re-use?
Thanks in advance
My app is getting crashed (throws OutOfMemoryError) when I try to load images in a ViewPager. Here are the details.
All the images are 720*1280 in dimension with 100-150KB in size.
I even tried using Fragments in a Viewpager to load images.. but after sliding one or two images the app gets crashed.
Any pointers on how to resolve this?
Thanks!!
720*1280*32 = 29491200 bits = 3.52 mb each one in memory.
Checkout this tutorial :)
Out of memory error means exactly what is stays. The application is trying to allocate to much memory. Android application are (depending on device) allowed to allocate 30-50MB.
The problem with the view pager is that it tries to hold at least 3 pages (current previous and next) to provide fluent UI work.
Bitmap to be displayed needs to uncompressed. So the only thing that matters is the size (pixel number) and color depth.
Usually 3 bitmaps of size you given should be quite easy hold on the device. I suppose that you are trying to process them in some not proper way, or you are missing releasing them from memory. Can't say more not knowing what are you doing with those bitmaps.
I'm creating a simple Gallery of drawables - each of them is almost the size of a screen, so they require quite much memory. For each entry I'm creating a custom LinearLayout with ImageView and TextView for the title. As most of you know, android Gallery doesn't recycle views so it gallery will crash easily on low-memory phones (after loading 4 drawables on 16mb ram limit, in my case).
Here's the simple question - how do you implement such gallery, so it won't run out of the memory? How do you recycle these images? A working code example would be great.
Few notes:
inSampleSize isn't a way to go, I can't scale these images down
Calling recycle() on Drawable's loaded from resource is impossible, as it will crash on Android 4.0+ (it will recycle the drawable in their internal cache)
Don't ask me to post the code, as there is no.
You shouldn't be using Gallery because it's deprecated. Especially since there isn't any code written so far. The documentation suggests using a HorizontalScrollView or ViewPager.
I feel a ViewPager is what your looking for because it will only keep at most 3 pictures in memory and handels all the recycling for you. Here is a post with more information about how to implement one android viewPager implementation