Immutable bitmap crash error

287 views
Skip to first unread message

emil.h....@gmail.com

unread,
Jul 1, 2014, 10:43:43 AM7/1/14
to codenameone...@googlegroups.com
I need an interactive chart, specifically to be able to draw labels on bars when the user clicks them. To get a good performance, i reuse the rendered chart image (rendered using aChartEngine) and draw the labels on top on a copy afterwards. The code looks something like this

public Image modifyImage(Image base, String s, int x, int y) {
Image copy = Image.createImage(base.getRGB(), base.getWidth(), base.getHeight());
Graphics g = last.getGraphics();
g.drawString(s, x, y);
return copy;
}

The code works perfectly in the simulator and on iOS, but not on Android. On Android an IllegalStateException: Immutable bitmap passed to Canvas constructor is thrown. The solution in "native Android" can be found here, but what to do in CN1?



If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA
Desktop OS
Simulator
Device

Steve Hannah

unread,
Jul 1, 2014, 10:48:48 AM7/1/14
to codenameone...@googlegroups.com
What line is the exception thrown on.  Your snippet shows you drawing to the Graphics context you obtained from "last" but you don't show what "last" is. My guess is "last" is not a mutable image.


--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/606090c0-1d5a-4f23-bd61-514ea96ebc06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Web Lite Solutions Corp.

emil.h....@gmail.com

unread,
Jul 1, 2014, 10:53:11 AM7/1/14
to codenameone...@googlegroups.com
That's a "bug in the post" when i copied it and renamed variables to make the code readable; last = copy (can i edit the original post?). Anyhow, is there any way I can make "copy" mutable (current it is on iOS and in simulator, but not on Android)? 


On Tuesday, July 1, 2014 4:48:48 PM UTC+2, shannah wrote:
What line is the exception thrown on.  Your snippet shows you drawing to the Graphics context you obtained from "last" but you don't show what "last" is. My guess is "last" is not a mutable image.
On Tue, Jul 1, 2014 at 7:43 AM, <emil.h....@gmail.com> wrote:
I need an interactive chart, specifically to be able to draw labels on bars when the user clicks them. To get a good performance, i reuse the rendered chart image (rendered using aChartEngine) and draw the labels on top on a copy afterwards. The code looks something like this

public Image modifyImage(Image base, String s, int x, int y) {
Image copy = Image.createImage(base.getRGB(), base.getWidth(), base.getHeight());
Graphics g = last.getGraphics();
g.drawString(s, x, y);
return copy;
}

The code works perfectly in the simulator and on iOS, but not on Android. On Android an IllegalStateException: Immutable bitmap passed to Canvas constructor is thrown. The solution in "native Android" can be found here, but what to do in CN1?



If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA
Desktop OS
Simulator
Device

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.

emil.h....@gmail.com

unread,
Jul 1, 2014, 10:55:17 AM7/1/14
to codenameone...@googlegroups.com, emil.h....@gmail.com
[EDT] 0:0:6,779 - Exception: java.lang.IllegalStateException - Immutable bitmap passed to Canvas constructor
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
	at android.graphics.Canvas.<init>(Canvas.java:83)
	at com.codename1.impl.android.AndroidImplementation.o(AndroidImplementation.java:1090)
	at com.codename1.i.p.h(Image.java:652)
	at dk.emhermunk.b.a.b.a(ChartViewCountModifier.java:41)
	at dk.emhermunk.b.a.c$1.c(ChartViewImpl.java:41)
	at com.codename1.i.n.c(Form.java:1997)
	at com.codename1.i.g.e(Component.java:2494)
	at com.codename1.i.k.l(Display.java:1862)
	at com.codename1.i.k.j(Display.java:999)
	at com.codename1.i.k.i(Display.java:930)
	at com.codename1.i.y.run(RunnableWrapper.java:120)
	at com.codename1.impl.b$1.run(CodenameOneThread.java:60)
	at java.lang.Thread.run(Thread.java:1096)

Steve Hannah

unread,
Jul 1, 2014, 11:16:14 AM7/1/14
to codenameone...@googlegroups.com, emil.h....@gmail.com
If you work with a debug build the stack traces will be easier to decipher.  I can't tell from what you've posted so far which line is causing the exception.

On a performance note, I plan to change aChartEngine to use the new graphics pipeline instead of pisces.  I'm hopeful that this will improve performance enough to have some more interactivity and animations in the charts.

Steve


To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

emil.h....@gmail.com

unread,
Jul 1, 2014, 5:03:37 PM7/1/14
to codenameone...@googlegroups.com, emil.h....@gmail.com
Ah, i didn't know that was even an option on android. Here is the "extended" stack trace:

[EDT] 0:2:7,556 - Exception thrown on EDT!
[EDT] 0:2:7,561 - Exception: java.lang.IllegalStateException - Immutable bitmap passed to Canvas constructor
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
	at android.graphics.Canvas.<init>(Canvas.java:83)
	at com.codename1.impl.android.AndroidImplementation.getNativeGraphics(AndroidImplementation.java:1090)
	at com.codename1.ui.Image.getGraphics(Image.java:652)
	at dk.emhermunk.gui.charts.ChartViewCountModifier.determineImage(ChartViewCountModifier.java:41)
	at dk.emhermunk.gui.charts.ChartViewImpl$1.pointerPressed(ChartViewImpl.java:41)
	at com.codename1.ui.Form.pointerPressed(Form.java:1997)
	at com.codename1.ui.Component.pointerPressed(Component.java:2494)
	at com.codename1.ui.Display.handleEvent(Display.java:1862)
	at com.codename1.ui.Display.edtLoopImpl(Display.java:999)
	at com.codename1.ui.Display.mainEDTLoop(Display.java:930)
	at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
	at com.codename1.impl.CodenameOneThread$1.run(CodenameOneThread.java:60)
	at java.lang.Thread.run(Thread.java:1096)

Use of the new graphics pipeline sounds great! Is there any ETA? BTW, I also noticed a performance increase running on iOS 8 (beta) compared to iOS 7.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsubscr...@googlegroups.com.



--
Steve Hannah
Web Lite Solutions Corp.

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.

Shai Almog

unread,
Jul 2, 2014, 1:40:00 AM7/2/14
to codenameone...@googlegroups.com, emil.h....@gmail.com
You can't invoke getGraphics() on an image unless it was created using one of:
Image.createImage(int width,int height) or Image.createImage(int width,int height, int argbFillColor)

Only those image objects return a "mutable" (modifable) instance of an image.

If you want to use RGB data with a mutable image you need to create that image then draw it onto the graphics of the mutable image.

emil.h....@gmail.com

unread,
Jul 2, 2014, 3:14:44 AM7/2/14
to codenameone...@googlegroups.com, emil.h....@gmail.com
That makes sense. Weird that it works in the simulator and on iOS then. Changing the code to:

public Image modifyImage(Image base, String s, int x, int y) {
Image copy = Image.createImage(base.getWidth(), base.getHeight(),0);
Graphics g = copy.getGraphics();
g.drawImage(base, 0, 0);
g.drawString(s, x, y);
return copy;
}

fixed the problem.

Shai Almog

unread,
Jul 2, 2014, 10:20:26 AM7/2/14
to codenameone...@googlegroups.com, emil.h....@gmail.com
We should probably improve the simulation of this behavior. Some platforms don't have these restrictions.

sakar

unread,
Jun 22, 2015, 2:14:26 AM6/22/15
to codenameone...@googlegroups.com, emil.h....@gmail.com
I created image from createImage(InputStream stream) which returns immutable image but i have to edit that image how can i??I cant create image with content using Image.createImage(int width,int height) or Image.createImage(int width,int height, int argbFillColor)

Shai Almog

unread,
Jun 22, 2015, 9:43:06 AM6/22/15
to codenameone...@googlegroups.com, sakar.ca...@gmail.com, emil.h....@gmail.com
I've explained to you before that you should not reopen old discussions not started by yourself.
Reply all
Reply to author
Forward
0 new messages