2

I am trying to capture webview full page content as a screenshot(bitmap) in android but no luck. Tested many solutions suggested in stackoverflow and other sites. Please help.

Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webView.getContentHeight(), Bitmap.Config.ARGB_4444);
webView.draw(new Canvas(bitmap));
Nagendra
  • 193
  • 4
  • 14

1 Answers1

0

You can try this :

    WebView w ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    w = new WebView(this);
    w.setWebViewClient(new WebViewClient()
    {
            public void onPageFinished(WebView view, String 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( "mnt/sdcard/yahoo.jpg" );
                            if ( fos != null )
                            {
                                b.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                                fos.close();
                            }
                        }
                   catch( Exception e )
                   {

                   }
          }
      });

    setContentView(w);
    w.loadUrl("http://search.yahoo.com/search?p=android");
}

I found this from hear.. click. did you try in this way.

Tanim reja
  • 2,120
  • 1
  • 16
  • 23
  • 1
    Yes but capturePicture method is deprecated in API level 19 and that method returns null on api level 19 or higher and as per the documentation "Use onDraw(Canvas) to obtain a bitmap snapshot of the WebView, or saveWebArchive(String) to save the content to a file." – Nagendra May 05 '15 at 12:43
  • -it creates image with a full height of the page, but only display contain is captured within screenshot, the bottom of screen shot will remain blank any idea? and Will it work if we load HTML data instead of WebURL ? – Hitesh Dhamshaniya Apr 10 '17 at 12:57