1

I have a webview that I want to turn into a bitmap for another part of my application.

Currently, I can get a bitmap of the portion of the webview that will be rendered onscreen, however my bitmap needs to contain all of the page's contents.

Setting the webview's layoutParams to a larger size results in the original image loading onto a larger canvas.

Here is my current implementation:

FrameLayout.LayoutParams tempParams= new FrameLayout.LayoutParams(2000,4000);
_webView.setLayoutParams(tempParams);
_webView.layout(0,0,2000,4000);
int width = _webView.getWidth();
int height = _webView.getHeight();

_webView.postDelayed(new Runnable() {
    @Override
    public void run() {
        Bitmap bitmap = Bitmap.createBitmap(_webView.getWidth(), _webView.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        _webView.draw(canvas);

    }
},100);

I have followed the following tutorials and they do not seem to resolve my issue.

Rendering Android webview to bitmap, html5 javascript , callback issue

Generate bitmap from HTML in Android

I was thinking about using a PictureListener and capturePicture() but those have been deprecated.

Community
  • 1
  • 1
Matt
  • 83
  • 1
  • 8

1 Answers1

5

You can set WebView height to wrap_contents, and it will resize itself to the content height.

You can still use PictureListener.onNewPicture() to receive a signal that WebView has drawn something. It is better than using onPageFinished(), as the latter only indicates that WebView has received all the page contents. The current status of PictureListener.onNewPicture() means that it's just not possible to obtain the picture from it, you need to use WebView.draw() as you are already doing.

Also, be aware that if your app is targeting L API level or above, you need to call WebView.enableSlowWholeDocumentDraw() prior to creating any WebViews in order to make it to draw the entire contents of WebView onto the canvas, see this.

Mikhail Naganov
  • 6,643
  • 1
  • 26
  • 26
  • Thanks for the response! Where should I be setting the WebView height to wrap_contents? I am currently using a custom WebView that I did not design, so once I'm done getting my image the WebView's attributes need to be restored. – Matt Jul 10 '15 at 16:31
  • Definitely before you are about to load something into it. The same code as you are already using, just use `WRAP_CONTENT` constant instead of a specific height value. – Mikhail Naganov Jul 10 '15 at 16:46
  • This doesn't seem to work for me. I still get the part of the webview that would have already been displayed on my device, not the entire web page. Thanks though. – Matt Jul 10 '15 at 18:35
  • 1
    If you have your WebView on screen, did you put it inside a `ScrollView`? Because otherwise, WebView just can't grow beyond the screen size. – Mikhail Naganov Jul 10 '15 at 19:43
  • 1
    I was targeting below API 21 but testing on a device running above that so once I enabled WebView.enableSlowWholeDocumentDraw() things started working out. Thanks again! – Matt Jul 14 '15 at 21:38