0

I've tried to find a better solution for create bitmap from WebView content, but I can't find a approach. I did try following this solution, and it works, however, some methods are deprecated like webView.setDrawingCacheEnabled(true) and webView.buildDrawingCache(). Is there any better solution to achieve it?

My second approach is to use webView.draw(canvas) like below, but it doesn't save the whole page WebView Content.

web.measure(View.MeasureSpec.makeMeasureSpec(
        View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
web.layout(0, 0, web.measuredWidth,
        web.measuredHeight)


val bitmap = Bitmap.createBitmap(web.measuredWidth, web.measuredHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
web.draw(canvas)
Seanghay
  • 1,076
  • 1
  • 11
  • 24

1 Answers1

1

You have to set WebView.enableSlowWholeDocumentDraw(); before you inflate your webview to get your entire webview to draw to a bitmap. This is for SDK version >= 21.

pale bone
  • 1,746
  • 2
  • 19
  • 26