2

I want to take a screenshot of a web view after the page loading finished. So I try to add the getSnapShot() in onPageFinished(), but when call the getSnapShot(), the page is not loaded. So the picture of snapshot is blank.

And I load a local html file url, so I believe there is no redirect.

I also tried to make MyWebView extends WebView, override onDraw(), add a callback listener to call getSnapShot() at onDraw() process. But still not working.

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        // will get a blank picture
        getSnapshot();
    }
});
Hasan Kucuk
  • 2,433
  • 6
  • 19
  • 41
defalt1996
  • 21
  • 1
  • you try without webview? maybe you are it is incorrect code using – Hasan Kucuk Apr 02 '19 at 09:01
  • getSnapshot() is a method of taking screenshot, familiar with your code. But the screenshot is blank when I call it in onPageFinished(), (I tried use a button to take screenshot manually when page displayed, it works out. But I want automatically take screenshot).... – defalt1996 Apr 03 '19 at 01:59

1 Answers1

1

Try this code;

 webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
    Picture picture = view.capturePicture();
        Bitmap  b = Bitmap.createBitmap( picture.getWidth(),
        picture.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas( b );

         picture.draw( c );
         FileOutputStream fos = null;
         try {

       fos = new FileOutputStream( "your_directory_screenshot.jpg" );
          if ( fos != null )
         {
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);

      fos.close();
               }
         }
        catch( Exception e )
      {}
    }
});
Hasan Kucuk
  • 2,433
  • 6
  • 19
  • 41