问题
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.
回答1:
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:
- xhdpi: 2.0
- hdpi: 1.5
- mdpi: 1.0
- ldpi: 0.75
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:
- Include the resource as
ldpi
and have it scale up instead, which doesn't affect the purpose as background. - Include the resource as
nodpi
drawable and it will never be scaled. - Use an XML resource instead.
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.
来源:https://stackoverflow.com/questions/15428441/android-resource-not-found-because-of-width-and-height