Is there a way to take a screenshot in codenameone (especially for Android)?

Viewed 243

I tried to take a Screenshot every second, to find out, if the user is in a menu of another app. I found a function in Display named capturePhoto(...) but it's a void function. I also tried it with Robot, but it isn't supported.

I googled it, but only found text not code to use.

The function I found: Display.getInstance().capturePhoto(null);

I don't know what goes there instead of null

1 Answers

To take a screenshot in Codename One, you can use the following static method:

    /**
     * Returns a screenshot of the currently displayed Form, or null if no form
     * is shown
     *
     * @return
     */
    public static Image getScreenshot() {
        Form form = Display.getInstance().getCurrent();
        if (form != null) {
            Image screenshot = Image.createImage(form.getWidth(), form.getHeight());
            form.paintComponent(screenshot.getGraphics(), true);
            return screenshot;
        } else {
            return null;
        }
    }

It works in all the platforms supported by Codename One, not only on Android.

Related