0

I use xhdpi resources in my app and let the system scale them to other dpis.
Among the other resources I have 1px wide png file as stretch background for a view.
The problem is that I received some crush reports from ldpi device users (like Galaxy Pocket) with the error like

Caused by: java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:603)
at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:649)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:624)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:476)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:775)
at android.content.res.Resources.loadDrawable(Resources.java:1974)
... 34 more

As I understand, my 1px wide png is causing this problem. 1 px in xhdpi is scaled to 0.375 px in ldpi, which I think is rounded to 0px. Currently I have just increased width of my resource to 5px, and tested on Galaxy Pocket, and it worked for now. But I would like to sort it out completely with your help. Is my guess correct?


The xml where I use it:

<LinearLayout
     android:id="@+id/info"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/ui_topbar_bg"
     android:gravity="center_vertical"
     android:orientation="horizontal"
     android:paddingRight="8dp">

@drawable/ui_topbar_bg is the image name

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
  • post imageview code. chances of displaying image later.. – NovusMobile Jul 20 '15 at 06:19
  • @NovusMobile I don't use `ImageView`, as I said, I use image as a `android:background` of the view. I updated the code. – Vladyslav Matviienko Jul 20 '15 at 06:22
  • I am interesting into the your java call. As I mentioned. only chance to get 0 .when you are calling it later one. – NovusMobile Jul 20 '15 at 06:32
  • @NovusMobile, I don't use it in Java code. That error appears when system is trying to scale resource itself. – Vladyslav Matviienko Jul 20 '15 at 06:38
  • may be this will help you : http://stackoverflow.com/questions/17605662/illegalargumentexception-width-and-height-must-be-0-while-loading-bitmap-from – NovusMobile Jul 20 '15 at 06:38
  • @NovusMobile, thanks, but I found that already. It is not what I'm asking about – Vladyslav Matviienko Jul 20 '15 at 06:40
  • Why not setting different images in different folder manually ,its not that hard – Sanjeev Jul 20 '15 at 07:19
  • @L-X, I know, but that's not an option for me since I'm not professional Photoshop (or like that) user, and can't scale it correctly, and the guy who created `xhdpi` resources won't be paid to scale them to other DPI – Vladyslav Matviienko Jul 20 '15 at 07:22
  • You just have to re-size the images , its fairly easy ,Now I would suggest you to use 2 px wide png , Can I know is your image width is 1px , and if yes then why not use color for background – Sanjeev Jul 20 '15 at 07:27
  • @L-X, as said in my question, width is 1 px, and it is not 1 x 1 image. It's height is not 1 px. – Vladyslav Matviienko Jul 20 '15 at 07:40
  • There are no other solutions, your images have to either be at least 2 x 2 px in size or you have to provide additional images for ldpi density. – Dalija Prasnikar Jul 20 '15 at 08:52
  • @DalijaPrasnikar, thanks. Any link to the official documentation? – Vladyslav Matviienko Jul 20 '15 at 09:34
  • 1
    [Supporting Different Densities](http://developer.android.com/training/multiscreen/screendensities.html) However, there is no official documentation that would specifically mention your issue. It is just matter of simple math, like you already concluded yourself. If you are providing only higher density images, you have to make sure that smallest dimension will be >= 1 px when downscaled. In your current case that is 2 * 0.375 = 0.75 rounded to 1 – Dalija Prasnikar Jul 20 '15 at 10:25

0 Answers0