UnsupportedException from Canvas.setBitmap (Bitmap)

I create an application that draws and under it under the control of users, and allows them to save it. The way I'm trying to achieve this is to use a custom Bitmap on which the canvas is drawn, and then save the resulting Bitmap.

Everything works as expected until Canvas.setBitmap (Bitmap) is called.

I get the following error.

03-24 13:47:50.741: E/AndroidRuntime(27888): FATAL EXCEPTION: main 03-24 13:47:50.741: E/AndroidRuntime(27888): Process: example.imageeditor, PID: 27888 03-24 13:47:50.741: E/AndroidRuntime(27888): java.lang.UnsupportedOperationException 03-24 13:47:50.741: E/AndroidRuntime(27888): at android.view.HardwareCanvas.setBitmap(HardwareCanvas.java:39) 

Code that throws an exception:

 protected void onDraw(Canvas canvas) { mResultImage=Bitmap.createBitmap(width,height,mOriginalImage.getConfig()); canvas.setBitmap(mResultImage); canvas.save(); if(mOriginalImage!=null) canvas.drawBitmap(mOriginalImage, width, height, paint); else canvas.drawText("Image loading...", width/2f-20, height/2, paint); canvas.drawText(text, x, y-20, paint); canvas.restore(); super.onDraw(canvas); } 

android.view.HardwareCanvas is not even mentioned in android. But I was able to find some information about this. It seems that setBitmap (Bitmap) has not been written yet, and that is normal.

My question is: why does onDraw (Canvas) return the HardwareCanvas class? This is not even a superclass for the canvas.

Bonus question: is it somehow?

+6
source share
1 answer

If you want to draw a bitmap, you must create a new canvas that passes it a bitmap. You should not be allowed to change the purpose of the canvas onto which your presentation should be drawn. So just create a new canvas with a bitmap, and then draw the resulting bitmap on your canvas in the onDraw method.

+5
source

All Articles