Android resource not found due to width and height

I recently posted an Android app in the store that worked great for almost all of my users. However, I soon began to receive several weekly crash reports 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 happening here. The only thing I could collect from user reports was that it apparently mostly occurred on devices with low-density displays. This particular resource is used as the background for presentation in the XML interface.

+7
source share
1 answer

The problem here is caused by an unsuccessful combination of automatically scaling extracted resources in Android and very small drawings.

If, for example, your application provides drawable-xhpdi , they should be reduced to fit lower density screens. This is done automatically by Android, if you do not provide these resources yourself.

The display density scale is set as follows:

  • xhdpi: 2.0
  • hdpi: 1.5
  • mdpi: 1.0
  • ldpi: 0.75

This means that your xhdpi resources xhdpi reduced by a factor of 2.0 on medium-density displays. This can be detrimental to quality, so it is usually recommended that you create and provide these resources with lower density yourself.

Now back to the problem. It is not unusual to have a resource with a very small width or height (1 or 2 pixels). An example use case is to create a solid colored background for presentation.

Unfortunately, when providing these resources as xhdpi and reducing their size, the pixel size can be truncated to 0. There is no protection for this, and Android will not be able to create a bitmap with this measurement and crash.

There are several solutions for this:

  • Include the resource as ldpi , and instead it scales, which does not affect the appointment as a background.
  • Include the resource as nodpi drawable and it will never scale.
  • Use an XML resource instead.

The latter option most accurately describes the intention and allows you to easily change color later 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.

+22
source

All Articles