Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalStateException while adding bitmap to Canvas

I was trying to set a bitmap image to a canvas using setBitMap ,at that time I got an IllegalStateException.This canvas have some images on it currently, I am trying to replace it. Any one have any idea why this happened?

Code Snippet

editBm = Bitmap.createBitmap(951, 552, Bitmap.Config.ARGB_8888);    
        Canvas mCanvas=new Canvas(editBm);
        eBit=LoadBMPsdcard(filePath); ---->returns a bitmap when the file path to the file is provided
        Log.i("BM size", editBm.getWidth()+"");
        mCanvas.setBitmap(eBit);

I am not getting any NullPointer errors and the method LoadBMPsdcard() is working good.

Please let me know about any ideas you have ...

Thanks in advance

Happy Coding

like image 297
rahul Avatar asked Jan 29 '26 23:01

rahul


2 Answers

IllegalStateException could be thrown because you're loading a Bitmap (eBit) and use mCanvas.setBitmap(eBit) without checking if the bitmap is mutable. This is requiered to draw on the Bitmap. To make sure your Bitmap is mutable use:

eBit=LoadBMPsdcard(filePath);
Bitmap bitmap = eBit.copy(Bitmap.Config.ARGB_8888, true);
canvas.setBitmap(bitmap);
like image 200
ZeroTek Avatar answered Feb 01 '26 14:02

ZeroTek


Try to use drawBitmap instead of the setBitmap. It looks like you've already set a bitmap to draw into by passing it to the canvas constructor, so now you just need to draw everything onto it.

like image 37
Konstantin Burov Avatar answered Feb 01 '26 15:02

Konstantin Burov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!