I've published an Android app in the store recently that worked perfectly for almost all of my users. However, I shortly started getting a few crash reports a week with the following trace:
Caused by: android.content.res.Resources$NotFoundException: File res/drawable-xhdpi/dark_button_unpressed.png from drawable resource ID #0x7f02005d
at android.content.res.Resources.loadDrawable(Resources.java:1716)
at android.content.res.Resources.getDrawable(Resources.java:581)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1696)
... 40 more
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
at android.content.res.Resources.loadDrawable(Resources.java:1711)
... 45 more
It is not easy to determine what exactly is going on here. The only thing I could gather from the user reports was that it seemed to mostly occur on devices with low density displays. This specific resource is used as background for a view in an XML UI.
The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc.
The resources locates inside res directory of your app. The Android resource compiler processes resources arcording to which subforder they are in and the format of the file, for example: res/drawable : Contains all resources file that can be drawable, such as (. png, .
Externalizing your resources also allows you to provide alternative resources that support specific device configurations such as different languages or screen sizes, which becomes increasingly important as more Android-powered devices become available with different configurations.
The problem here is caused by an unfortunate combination of the automatic scaling of drawable resources in Android and very small drawables.
If, for example, your app only provides drawable-xhpdi
resources, they need to be downsized to fit lower density screens. This is done automatically by Android if you don't provide these resources yourself.
The scale of display densities is set to be like this:
That means that your xhdpi
resources are scaled down by a factor 2.0 on medium density displays. This can be detrimental for the quality, which is why it is usually recommended to create and provide these lower density resources yourself.
Now, back to the problem at hand. It is not entirely uncommon to have a resource that has a very small width or height (1 or 2 pixels). An example of a use case is providing a solid color background for a view.
Unfortunately, when providing these resources as xhdpi
and scaling them down, the pixel size can be truncated to 0. There is no guard in place for this and Android will fail to create a Bitmap with that dimension and crash.
There are several solutions for this:
ldpi
and have it scale up instead, which doesn't affect the purpose as background.nodpi
drawable and it will never be scaled.The last option most accurately describes the intent and allows you to easily change the color later on without an image editing program:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#AARRGGBB" />
</shape>
Include this resource in the drawables
folder and it will always work properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With