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

后端 未结 3 1450
太阳男子
太阳男子 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)

提交回复
热议问题