title: How to convert a HTML string to an image in Android AsyncTask?
body: <p>is there anybody know how to convert a HTML page to an image in Android AsyncTask or background thread?</p>
  
  <p>Environment: Android Studio 2.0 Beta 5, Android Tablets with API version 18, 19</p>
  
  <p>I have tried WebView control with two solutions but all failed.</p>
  
  <ul>
  <li>Solution 1: Javascript</li>
  </ul>
  
  <p>It doesn't work, getDrawingCache(true) always returns null.</p>
  
  <pre><code>public void generateImage()
  {
      final WebView mWebView = new WebView(this);
      mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
      mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
      mWebView.setInitialScale(150);
      mWebView.getSettings().setLoadWithOverviewMode(true);
  
      mWebView.getSettings().setJavaScriptEnabled(true);
      mWebView.setDrawingCacheEnabled(true);
      mWebView.addJavascriptInterface(new JavaScriptInterface(mWebView), "Android");
  
      String html = "&lt;html&gt;&lt;body onload=\"Android.captureImage()\"&gt;hello world&lt;p&gt;bbb&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;";
      mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
  }
  
  public class JavaScriptInterface 
  {
      private WebView mWebView;
  
      public JavaScriptInterface(WebView webView) {
          mWebView = webView;
      }
  
      @JavascriptInterface
      public void captureImage()
      {
          Bitmap b = mWebView.getDrawingCache(true);
          if(b==null){
              System.out.println("... b is null");
              return;
          }
          System.out.println("... height "+b.getHeight()+"    width "+b.getWidth());
      }
  }
  </code></pre>
  
  <ul>
  <li>Solution 2: WebViewClient + PictureListener</li>
  </ul>
  
  <p>This one works on Android API 19. But on Android API 18, the callback "onNewPicture" is never get called. </p>
  
  <pre><code>public void generateImage()
  {
      final WebView mWebView = new WebView(this);
      mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
      mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
      mWebView.setInitialScale(150);
      mWebView.getSettings().setLoadWithOverviewMode(true);
  
      String html = "&lt;html&gt;&lt;body&gt;hello world&lt;p&gt;bbb&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;";
      mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
  
      mWebView.setWebViewClient(new WebViewClient() {
          @Override
          public void onPageFinished(WebView view, String url) {
              System.out.println("............ onPageFinished");
              super.onPageFinished(view, url);
              view.clearCache(true);
          }
      });
  
      mWebView.setPictureListener(new WebView.PictureListener() {
          @Override
          public void onNewPicture(WebView view, Picture picture)
          {
              if (picture == null) {
                  return;
              }
  
              Picture pic = mWebView.capturePicture();
              if(pic==null){
                  System.out.println("............ pic is null");
                  return;
              }
              System.out.println("onNewPicture............ width ["+pic.getWidth()+"]  hieght ["+pic.getHeight()+"]");
              final Bitmap bm = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(), Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(bm);
              canvas.drawPicture(pic);
          }
      });
  }
  </code></pre>
  
  <p>I can get image if I create WebView control in following way</p>
  
  <pre><code>final WebView mWebView = (WebView) findViewById(R.id.webview);
  </code></pre>
  
  <p>But I have to convert the html page string in an AsyncTask or a background thread. there is no UI at all. Is there anybody know how to implement it?</p>
  
  <p>Any help is appreciated.</p>