Trying to get the bitmap of a canvas in an extension

I'm working on an extension where I pass a canvas to the extension.
In the extension I draw something on the canvas.
Then I try to get the bitmap of the canvas.

		canvas.BackgroundColor(Color.CYAN);
		canvas.PaintColor(Color.GREEN); 
		canvas.DrawCircle(100, 100, 50f, true);
		Bitmap bm = canvas.getBitmap();

In Eclipse I have no compile errors, in Rush I get the following error:

  • erro src\be\naafets\testbitmap\Testbitmap.java:29: cannot find symbol
    │ Bitmap bm = canvas.getBitmap();
    │ ^
    │ symbol: method getBitmap()
    │ location: variable canvas of type Canvas
    └ failed*

In the sourcefile of canvas.java, the method getBitmap() seems to be added explicitly for use in extensions...
(and my real target extension is much more complex, above snippet is only to demonstrate the error).
What am I doing wrong?

This method has not been added in Rush.
Download AndroidRuntime.jar file and copy to deps folder.

This did not solve the problem.
I downloaded AndroidRuntime.jar and added it to the deps folder of my Rush project and to yaml.xml

# Extension dependencies (JAR).
deps:
  - AndroidRuntime.jar

And I receive the same error.

correction: I added it to rush.yml, don't know why I wrote yaml.xml :grin:

Have you executed the command:
Rush deps sync

I think you should update Rush. The newer version of Rush has a "comptime_dependencies:" section in rush.yml which will be more appropriate for this .jar.

Which version of Rush do I need to execute this command? :thinking:


Navigate to:
C:\Users\<user>\AppData\Roaming\rush\dev-deps\rush

and replace runtime.jar with AndroidRuntime.jar. (rename it if doesn't work)

1 Like

No no you are doing it wrong. You actually dont need to replace any jar file on your computer manually.

You just need to add your AndroidRuntime.jar in your deps folder and thats it! It is a compile time dependency in this case and not runtime!

Doing this will override the existing sources while compiling.

That command is only available in the beta version of Rush :grin:

I tried all suggested solutions, only the one where I replace runtime.jar with AndroidRuntime.jar in the rush folder C:\Users\<user>\AppData\Roaming\rush\dev-deps\rush did work.
The problem however is that there are a lot of differences between the versions ofcanvas.java in of above jar files, probably explictly made by @shreyash for the use in rush. Although this solution works, it is not recommended according to me.
Finally I implemented the logic of getBitmap in my extension itself and everything is going well.

		int width = canvas.getView().getWidth();
		int height = canvas.getView().getHeight();
		Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		android.graphics.Canvas c = new android.graphics.Canvas(bm);
		canvas.getView().layout(0, 0, width, height);
		canvas.getView().draw(c);

1 Like

I used this (reflect) in my canvas to base64 extension:

import java.lang.reflect.*;
import com.google.appinventor.components.runtime.Canvas;
.
.
.
  @SimpleFunction(description = "Get Canvas Image and returns base64 string")
  public void GetCanvasImageAsBase64(final Canvas canvas ) throws Exception {
    final Method method = canvas.getClass().getDeclaredMethod("getBitmap");
    Bitmap imageBitmap = (Bitmap) method.invoke(canvas);
    String encodedString = bitMapToBase64(imageBitmap);
    AfterCanvasBase64(encodedString);
  }
1 Like