Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The takeScreenshot API did not achieve its intended effect #2365

Open
1 of 2 tasks
peter100u opened this issue Oct 22, 2024 · 3 comments
Open
1 of 2 tasks

The takeScreenshot API did not achieve its intended effect #2365

peter100u opened this issue Oct 22, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@peter100u
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I am developing an app for processing creative programming, and I want to use webview to take screenshots of content as a way to share my work. However, when I took the screenshots, they were ineffective and left blank

Expected Behavior

Can effectively capture screenshots of webview content

Steps with code example to reproduce

Steps with code example to reproduce
class TestScreenShot extends StatefulWidget {
  const TestScreenShot({super.key});

  @override
  State<TestScreenShot> createState() => _TestScreenShotState();
}

class _TestScreenShotState extends State<TestScreenShot> {
  InAppWebViewController? webViewController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          TextButton(
              onPressed: () {
                webViewController?.takeScreenshot().then((screenshot) async {
                  if (screenshot == null || webViewController == null) {
                    print('data is empty');
                    return;
                  }
                  Directory tempDir = await getTemporaryDirectory();
                  File file = File('${tempDir.path}/screenshot.png');
                  print('path ' + file.path);
                  await file.writeAsBytes(screenshot);
                  showDialog(
                      context: context,
                      builder: (context) {
                        return AlertDialog(
                          content: Image.memory(screenshot),
                        );
                      });
                });
              },
              child: Text('screenShot')),
        ],
      ),
      body: InAppWebView(
        initialUrlRequest: URLRequest(
          url: WebUri("https://processing-30afc.web.app/p5Preview"),
        ),
        onProgressChanged: (InAppWebViewController controller, int progress) {
          if (progress == 100) {
            webViewController = controller;
          }
        },
      ),
    );
  }
}

Stacktrace/Logs

Did I save the image locally while running the screenshot? It was successful
path /data/user/0/com.code.art.show.processing/cache/screenshot.png

Flutter version

3.24.3

Operating System, Device-specific and/or Tool

android,ios,xcode

Plugin version

6.1.5

Additional information

No response

Self grab

  • I'm ready to work on this issue!
@peter100u peter100u added the bug Something isn't working label Oct 22, 2024
@pichillilorenzo
Copy link
Owner

iOS is working for me. Instead, Android won't work with HTML canvas objects unfortunately.
On Android, you can try to use the controller.evaluateJavascript method and get the image data of the canvas using https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL javascript API.

@peter100u
Copy link
Author

Is it possible to use native APIs to take screenshots of your webview instead of finding ways to use JavaScript related APIs

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Oct 24, 2024

As I said, Android won't work with HTML canvas objects.
I have tried but didn’t found any solution for it.
Android WebView native APIs works but except for canvas elements that remains white.
This is has something to do with the API itself or how the platform manages it.
If you have or find any native solution for it, you are welcome! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants