Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android decodeResource returns null for xml bitmap - API level 7

Tags:

android

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

like image 580
Łukasz Zwierko Avatar asked Dec 03 '11 19:12

Łukasz Zwierko


2 Answers

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.

like image 94
Rafael Avatar answered Oct 17 '22 04:10

Rafael


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();
    }
like image 23
theJosh Avatar answered Oct 17 '22 04:10

theJosh