So this app I am working on, works just fine on a really old miserable Android 2.3.3 phone. However when running it on the GS4, the GS4 is throwing out of memory exceptions
(@Arash's answer provides a workaround, but this is my attempt to explain why it may work.)
So you have a rather big image in the drawable
resource directory, you reference it in your layout xml file, and get an out of memory error at runtime when inflating it.
When loading drawable resources, Android will perform some pre-scaling if it deems it necessary. From the official documentation:
Based on the density of the current screen, the system uses any size- or density-specific resources from your application and displays them without scaling. If resources are not available in the correct density, the system loads the default resources and scales them up or down as needed to match the current screen's density. The system assumes that default resources (those from a directory without configuration qualifiers) are designed for the baseline screen density (mdpi), unless they are loaded from a density-specific resource directory.
This means 2 things:
drawable
directory are assumed to be targeting an mdpi
densitySo you're currently running on an xxhdpi phone. The system wants to load the learn_more
drawable resource to paint it on the ivLearnMore
widget. It will look for it in the drawable-xxhdpi
in priority but won't find it there, so will take the closest one it finds, in this case the one in the drawable
directory (which it assumes to be targeting mdpi density), and load it into memory scaling it by a factor of 3 which is a lot if your image file was significantly big and can easily require more memory than is available to the application (and cause an out of memory error).
This is also why it loaded fine on your old crappy 2.3 device: the device was most likely of mdpi or hdpi density and the system didn't try to upscale it or did it by a factor of only 1.5.
The main thing here being that you put a resource suitable for an xxhdpi (or more ?) density in an mdpi directory.
So you have several options:
drawable-*
directory): this is the best to avoid any problem, when possibledrawable
being equivalent to drawable-mdpi
!), in your case maybe drawable-xxhdpi
(depends on your resource)drawable-nodpi
directory, this will prevent the system from performing any pre-scaling of the resource (but this is typically for density-agnostic resources e.g. which you resize yourself at runtime)I have discovered the problem. Background images are apparently not handled very efficiently, especially not for a 1920x1080 screen. Therefor you should use a custom xml drawable, a flat color, or a 24-bit jpg. Funny enough I just ran some tests and a 1920x1080 jpeg takes up 95.60 KB while the same image rendered as a png takes up 929.09 KB.
So I did some testing and found that xml defined drawables with gradients etc. are super fast, I then tried my 1920x1080 jpg and it was super fast, so then I put in a 1920x1080 png and got the error. This is funny to me because the png I has set at the start for HD screens was only 910x540 and I would get the error.
So lesson here is if you want a image background use a jpg.
I had the same problem and that was : Galaxy S4
and all 1920*1080 resolution devices , check in xxhdpi
folder first and if there is not specific images,it will scale other sizes like xhdpi
,... so i moved my pictures to xxhdpi
folder and it Solved!