galaxy s4 and maybe all HD phones? out of memory error inflating layout

后端 未结 3 1439
太阳男子
太阳男子 2020-12-16 04:21

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

相关标签:
3条回答
  • 2020-12-16 04:46

    (@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:

    1. If there's no resource of the density of the environment the application is currently running on, the system will take a resource of another density and scale it (up or down) to match the target density
    2. Resources put in the drawable directory are assumed to be targeting an mdpi density

    So 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:

    1. provide one resource appropriately sized per density (i.e. one in each drawable-* directory): this is the best to avoid any problem, when possible
    2. if you only have one version of the resource, put it in the directory matching its density (drawable being equivalent to drawable-mdpi!), in your case maybe drawable-xxhdpi (depends on your resource)
    3. put the resource in the 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)
    0 讨论(0)
  • 2020-12-16 04:57

    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.

    0 讨论(0)
  • 2020-12-16 05:05

    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!

    0 讨论(0)
提交回复
热议问题