10
votes

I've a curious situation: after moving my PNGs from /drawable-hdpi/ to /drawable/ and putting XML bitmaps to /drawable-hdpi/ instead, I can not decode these bitmaps with BitmapFactory.decodeResource() method - it returns null. What is stranger is that:

  1. I can decode actual the png file with this method (giving the actual drawable id)
  2. I can get the drawable using context.getResources().getDrawable(xml_id) -
  3. All ImageViews components are decoded correctly (but thats due to p. 2 I suppose)

What I see in the logcat is :

12-03 16:18:13.557: D/skia(2566): --- SkImageDecoder::Factory returned null
12-03 16:18:13.557: D/skia(2566): --- SkImageDecoder::Factory returned null
12-03 16:18:13.567: D/skia(2566): --- SkImageDecoder::Factory returned null

so I would take a wild guess that the decoder is given the xml file to decode instead of the actual resource (which I checked is valid).

Any hints? Is it possible to BitmapFactory.decodeResource() with the xml bitmap? Btw, I'm using API 7.

And I've also tried to put the origina lpngs into drawable-nodpi but that did not help either. thanks

4

4 Answers

8
votes

Get the drawable and then convert to Bitmap:

Drawable drawable = getResources().getDrawable(resId);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

Now you can use the Bitmap object.

1
votes

An XML bitmap is a resource defined in XML that points to a bitmap file. The effect is an alias for a raw bitmap file. The XML can specify additional properties for the bitmap such as dithering and tiling.

http://developer.android.com/guide/topics/resources/drawable-resource.html

I am having the same problem but I use this as a workaround. Unfortunaltly with this method I don't see any way to pass Options when you decode the stream.

            //init input stream
    is = getContext().getResources().openRawResource(resID);

    Bitmap returnBitmap;

    //Load bitmap directly - will fail if xml
    Bitmap newBmp = BitmapFactory.decodeStream(is, options);
    if(newBmp == null){
        //Load bitmap from drawable auto scales
        newBmp = ((BitmapDrawable) getContext().getResources().getDrawable(resID)).getBitmap();
    }
0
votes

What in the world is an XML bitmap? BitmapFactory.decodeResource() loads a bitmap image -- you should use a PNG or JPEG image with it, nothing else.

And please stop throwing random stuff in to -hdpi and -nodpi or whatever. For a given resource name, all of the different dpi qualifiers or whatnot provide different variations on the same thing. You shouldn't have some of them be bitmaps and some of them XML files, nor does it make sense to mix -nodpi with other variations.

0
votes

This Error also accures when trying to link an xml bitmap to another xml bitmap like this

<bitmap
  android:src="@drawable/glow"
  android:tileMode="disabled" android:gravity="center" >
</bitmap> 

where "glow" is another xml bitmap.