Image allocation OutOfMemoryError and crash in Android - android

I've a problem with my app relate to OutOfMemoryError and crash. I've realized two activity, first activity with a logo used to load application and a second activity with some buttons and a popup menù with some icons. Recently I've encountered problem with this popup menù that when is opened cause a crash with "OutOfMemoryError" warning. Before this crash I've used this menù without errors with only a single image repeated as test. When I've changed this image with different images it started to crash. I've added this android:largeHeap="true" to manifest too see if something changes and finally menu opens without crash but it lags when scrolling it. In application monitoring I've 112Mb used when menu is closed that goes to 204Mb when I open it. I've 115 images inside my drawable(mostly unused), about 35 active in my current Activity and additional 16 images when I open menu. Total images size is 4Mb, total used images size is 1Mb and total menu images size is 350Kb. I'm a newbie in Android but maybe I skip some conversion between png and memory in Android, but it's strange to me that 16 images(ImageButton) of 24kB with android:layout_width="85dp" android:layout_height="90dp" occupy so much memory and makes lag a new age smartphone. With this low sizes and no particular code(just xml files and some java to struct UI), how it's possible that I've this big numbers in my memory?

maybe you don't follow android standard size of icons, or the image is too large.
check this link for more info on how to use standard size in android : http://iconhandbook.co.uk/reference/chart/android/
Or you can use Universal Image Loader to display image see this link : https://github.com/nostra13/Android-Universal-Image-Loader

Related

Appropriate Background Image Size

My app keeps crashing due to an "Out of memory" exception. I'm almost positive that it has something to do with all of my images used within the app. I threw them in my drawable folder in the beginning without ever considering their size. For example, I use a background .png image in all of my Fragments. It's size is 832x561. Is that too large for say a standard android phone like a nexus 5? Is there a reference chart for appropriate image size based on device? Thank you.

Out of memory exception on inflating layouts

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

How to use listview to render huge images?

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"

loading more100 images in to dynamic button from drawable folder, out of memory error while using more than 150 images from drawable folder in android

i am developing kids 1 to 100 learning number speaching application. here i used nearly 100 images used to refer the every images.
i have two two levels. dynamically creating buttons, runtimely load images into buttons here.
one is for number showing with images level.
another one is for alphabetic with images showing level.
totally i used 150 images from drawable folder.all images are PNG format with width and height is nearly each one 240 into 210 like that..
when i click NUMBERS level button , that part working fine. when i get back second alphabetic level its completely application crashed.
Error is: Out of Memory error only..
I refered this stack link : Strange out of memory issue while loading an image to a Bitmap object
they told like, get the image and decode and load it into drawable.
here exactly i dont know , how to get image from drawable and load into bitmap and decode the image and set into dynamic button..
note: here i dynamically created 100 buttons using for loop, that time, i need to load images.
i created successfully everything. when i run the program, i am getting memory error.
please help me solve this issue..
I Solved this problem using android:largeHeap="true" in application tag in AndroidManifest.xml

android out of memeory exception for multiple image views

Hi all
I am developing app with two dynamic layouts and image views.when i am loading many images in image views, the first activity started well.But when i am open second activity i am getting
out of memory exception .
So please give me solution
Thanks in advance.
Are you loading lots of images as bitmaps in your code? They can become quite large, and you've only got the 16mb of heapspace to play with.
If you are using some sort of createBitmap function from bitmapFactory, check if you can set the sample size (inSampleSize I think) in the options.

Categories

Resources