3

I used to use capturePicture() method to make a snapshot of my WebView. This method was deprecated in API level 19.

The doc say that "Use onDraw(Canvas) to obtain a bitmap snapshot of the WebView", but I really don't know how it means.

Will you please teach me how to solve the problem?

Kara
  • 6,115
  • 16
  • 50
  • 57
Richard Guan
  • 137
  • 2
  • 7

1 Answers1

4

The following should work:

float scale = webView.getScale();
int webViewHeight = (int)(webView.getContentHeight() * scale);
Bitmap image = Bitmap.createBitmap(webView.getWidth(), webViewHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(image);
webView.draw(canvas);
loeschg
  • 29,961
  • 26
  • 97
  • 150
Richard Guan
  • 137
  • 2
  • 7
  • 3
    Do you capture entire the webview or just things which are visible? I meant if the web content is long, can your method capture all the entire? – lolyoshi Jan 06 '14 at 14:15
  • @lolyoshi `WebView.draw()` will indeed capture all the content, but I'm not sure if `WebView.getWidth()` will return the correct width for the content, I still have to check that for the app I'm working on. – JAB Mar 07 '14 at 20:56
  • Yes! Using getContentHeight() * getScale() does captures entire page not just a visible content. – Pioneer Mar 20 '15 at 05:31
  • 1
    On Android 6.0 it does NOT capture the entire view, only the visible part. – Patrick Nov 23 '15 at 18:10
  • getScale() is deprecated – Abhay Sood Nov 25 '15 at 13:11
  • out of memory exception. can anyone help please. I just need to capture visible area. Also `getScale()` is deprecated – Surinder Feb 02 '16 at 14:20