Fixed Bitmap Failure Error

java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor at android.graphics.Canvas.<init>(Canvas.java:127) at app.test.canvas.StartActivity.applyFrame(StartActivity.java:214) at app.test.canvas.StartActivity$1.onClick(StartActivity.java:163) at android.view.View.performClick(View.java:4223) at android.view.View$PerformClick.run(View.java:17275) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4898) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) at dalvik.system.NativeStart.main(Native Method) 

I get this error when crashing from the developer console. I do not understand what the problem is.

  BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inScaled = true; opt.inPurgeable = true; opt.inInputShareable = true; Bitmap brightBitmap = BitmapFactory.decodeResource(getResources(), position, opt); brightBitmap = Bitmap.createScaledBitmap(brightBitmap, 550, 550, false); chosenFrame = brightBitmap; Bitmap workingBitmap = Bitmap.createBitmap(chosenFrame); workingBitmap = Bitmap.createBitmap(workingBitmap); Canvas c = new Canvas(workingBitmap); 

I think this is due to this?

+54
android bitmap
Oct 29 '12 at 10:15
source share
3 answers

You need to convert workingBitmap to Mutable Bitmap to draw on Canvas . (Note: this method does not help save memory, it will use additional memory)

 Bitmap workingBitmap = Bitmap.createBitmap(chosenFrame); Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(mutableBitmap); 

This answer helps not waste memory Converting an immutable bitmap to a mutable bitmap

+161
Oct 29 '12 at 10:25
source share

BitmapFactory.decodeResource() returns an immutable copy of the bitmap, and you cannot draw it on canvas. To get your canvas, you need to get a modified copy of the raster image of the images, and this can be done with the addition of a single string code.

 opt.inMutable = true; 

Add this line to your code, and it should fail.

+29
Oct 28 '13 at 22:22
source share

to minimize memory usage, you can check this message about converting / decrypting the changed bitmap directly from the resources:

stack overflow

0
Aug 24 '13 at 10:07 on
source share



All Articles