1

I'm trying to load a webview with some html code and then capture it and turn into a Bitmap. However, all I can see in the ImageView I set to test it, is a blank square.

webViewToPrint.loadDataWithBaseURL("", dataStringHtml , "text/html", "UTF-8", "");
webViewToPrint.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
              super.onPageFinished(view, url);

              Bitmap b = Bitmap.createBitmap( view.getHeight(), view.getWidth(), Config.ARGB_8888);
              Canvas canvas = new Canvas( b );
              view.draw(canvas);
              imageView.setImageBitmap(b);       
        }});
BVtp
  • 2,308
  • 2
  • 29
  • 68
  • 1
    i this may help you :http://stackoverflow.com/questions/30409630/how-to-capture-the-webview-to-bitmap-in-android-5-0 – santoXme Dec 28 '15 at 09:06

3 Answers3

1
 webViewToPrint.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 );
                    imageView.setImageBitmap(b);    }}   
Javad Jafari
  • 710
  • 6
  • 11
  • the imageView is empty.. now it doesn't even show a white rectangle like it used to – BVtp Dec 28 '15 at 09:11
  • what version u are testing? add this one also hope to help if (Build.VERSION.SDK_INT >= 21) { webview.enableSlowWholeDocumentDraw (); } make sure you call this method before calling setContentView() in your onCreate() – Javad Jafari Dec 28 '15 at 09:15
  • My device is API 19, so I can't use enableSlowWholeDocumentDraw() – BVtp Dec 28 '15 at 09:17
1

Thank you guys for the help. The solution tough was to add another thread (postDelayed) inside onPageFinished with some delay (500 worked fine in my case).

BVtp
  • 2,308
  • 2
  • 29
  • 68
0

Use the following code:

webViewToPrint.setDrawingCacheEnabled(true);

In onPageFinished()

Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
imageView.setImageBitmap(bitmap);
Faraz
  • 2,144
  • 1
  • 18
  • 28