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:
context.getResources().getDrawable(xml_id)
- 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
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With