Bitmap decoding returning null - android

final Bitmap b = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
I am getting a null references error.
B is null.
Why is that?

Good question i went in the exact same issue (with the exact same file :)
It seems that
BitmapFactory.decodeResource works quite perfect with R.drawable and R.raw but not with mipmaps probably caused by the different screen resolutions, but this is just a guess.
Looking in the source i can only see that decodeResource calls Resource#openRawResource(..) and the documentation says:
... asset file -- that is, it can be used to open drawable, sound, and
raw resources;it will fail on string and color resources.
i guess mipmap files are not considered as raw resources as there is additional handling for choosing density dependent resources.
So you will have to specify the density of the resource: https://stackoverflow.com/a/41910618/8524651
Or quick and dirty just move a copy of that file to your raw folder.
The solution code in kotlin (decide for one way ;)
val launcherDrawable = ResourcesCompat.getDrawableForDensity(context.resources, R.mipmap.ic_launcher, DisplayMetrics.DENSITY_LOW, context.getTheme());
var bm = BitmapFactory.decodeResource(context.resources, R.raw.ic_launcher)
bm = launcherDrawable!!.toBitmap(launcherDrawable.minimumWidth, launcherDrawable.minimumHeight)

This may help you
Drawable d = getResources().getDrawable(R.mipmap.imagefile);
Bitmap b = ((BitmapDrawable)d).getBitmap();

Related

BitmapFactory.DecodeResource does not recognise drawables

I'm trying to load a simple image as a background for a GPS app using C# in Visual Studio.
The resources are located in
Resources/drawable/resource file
Now I've been trying to display these files like this, "Arrow" en "Map"
Kaart = BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.Map, opt);
Pijl = BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.Arrow, opt);
I've declared "Kaart" and "Pijl" als bitmaps at the start of the class but "Map" and "Arrow" remain unrecognised and are red underlined saying that Resource.Drawable does not contain a definition for "Arrow" and "Map".
Thanks!
Resources res = getContext().getResources();
int id = R.drawable.image;
Bitmap b = BitmapFactory.decodeResource(res, id);
This shall return the decoded Bitmap or it will return null if the image cannot be decoded.
NOTE : The Bitmap would be different if "image" exists in all
drawable folders(like ldpi,mdpi,hdpi , etc) . So it's ideal to keep the
image in the original drawable folder unaltered.

Android - png resource converted to a ColorDrawable on android < 4.0

According to documentation here, A png resource should be converted to a BitmapDrawable. However I'm observing a strange behavior wherein a png file which has only black pixels in it is resulting in a crash because of ClassCastException (wrapped in InvocationTargetException) if I try to do the following in a Custom View's constructor :
...
tempDrawable = typedArr.getDrawable(R.styleable.CustomView_src); // Source points to a png file
Log.i("TestPNGToResource", "Canonical Class Name " + tempDrawable.getClass().getCanonicalName());
tempBitmap = ((BitmapDrawable) tempDrawable).getBitmap();
...
I see the following logged on android 2.2 and 2.3
09-24 13:21:37.575: I/TestPNGToResource(532): Canonical Class Name android.graphics.drawable.ColorDrawable
Why is the resource not being converted to a BitmapDrawable?
This is an optimization performed by aapt. It is able to recognize images made of a single color and turn them into, well, a single color instead of a bitmap. This is more space-efficient, improves loading times, etc.
i assume this won't occur for 9-patch images, right? also, when using the image file, will extracting the apk file still show the image file inside it?
anyway, i'm not sure if that helps, but have you tried to check this value:
TypedValue value = a.peekValue(R.styleable.CustomView_src);
then , if it's any of the color types, you can create your own drawable with this color. problem is how to get the size if that's what you get.
however, if in the special case you've shown, you get a reference type, you can probably force it to be BitmapDrawable by just creating the bitmap using BitmapFactory.decodeResource().

Where is this drawable located?

I used the code from this answer, and compiled everything successfully.
However, I cannot find out how to use the drawable that I have just created. Here is my code:
Drawable dr2 = getResources().getDrawable(android.R.drawable.ic_menu_manage);
Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
Drawable f = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap2, 256, 256, true));
private Integer[] menu_icon = {
android.R.drawable.f,
// I can't figure out how to call Drawable f
};
After converting the drawable to the Drawable f, I am unable to find out how to recall the resized drawable. I have tried using all possible locations in my code as to where it may be (/res/drawable and a few other locations). However, I am unable to compile no matter how I try to call Drawable f. Any help would be greatly appreciated!
(And no, I have eight elements in that array, but only copy+pasted one)
It exists only in memory, not anywhere on disk. You can't reference it by id. Instead, you need to call versions of functions that take a Drawable rather than ones that take an int resourceID.

android decodeResource returns null for xml bitmap - API level 7

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:
I can decode actual the png file with this method (giving the actual drawable id)
I can get the drawable using context.getResources().getDrawable(xml_id) -
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
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();
}
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.
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.

android getting pictures from drawables

Bitmap picture = BitmapFactory.decodeResource(getResources(),R.drawable.phscale);
apart from above code any other ways to get the bitmap from drawables
If you get your png name as string, you could use the following:
int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");
source
If you would like to have full control over resource bits, you'd better put it either in res/asset or res/raw folders and then get access to them as:
InputStream is=this.getResources().getAssets().open("drawable.png");
//
is=this.getResources().openRawResource(R.raw.myDrawable);
See th link
How to convert a Drawable to a Bitmap?

Categories

Resources