2

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

1
  • post your logcat and LoadBMPsdcard() method Commented May 10, 2011 at 7:41

3 Answers 3

6

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

Canvas.setBitmap() throws IllegalStateException if and only if Bitmap.isMutable() returns true. Bitmap.createBitmap() builds an immutable Bitmap instance only, in all of its forms. To create a mutable bitmap you either use new Bitmap(), or Bitmap.copy(true), depending on whether you have a source bitmap that you want to start with. A typical block for me looks like:

Bitmap image = ...
Canvas c = new Canvas(image.isMutable()?image:image.copy(true));
...

This assumes, of course, that you don't mind clobbering the source Bitmap (which I generally don't but that's by no means universal).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.