title: Android best way to generate an image from a layout engine (justified text, box positioning, embed images...)
body: <p>It's been three days I'm looking for (and test) libs which can perform a generation of an <strong>image</strong> from <strong>textual content</strong>. I develop an app on <strong>Android</strong> with the Augmented Reality <a href="http://www.metaio.com/SDK/" rel="nofollow noreferrer">Metaio SDK</a>. To apply a texture with text and image embed, I <strong>must</strong> generate a PNG file. So I need a method :</p>
  
  <ul>
  <li>Compatible with <em>Android</em></li>
  <li>On the Android <em>client</em> : not generating the PNG on a server</li>
  <li>With the <em>right way</em> like html/css, svg or Android layout for example : ie not having to make justify text and box positionning from scratch, I'm sure you understand why x)</li>
  <li>Which can perform a generation in external thread (ie without the user have to see the layout)</li>
  </ul>
  
  <p>I tested a lot of solutions, but nothing works.</p>
  
  <p><strong>Using WebView</strong></p>
  
  <p>Some topics about this :</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4633988/generate-bitmap-from-html-in-android">Generate bitmap from HTML in Android</a></li>
  <li><a href="https://stackoverflow.com/questions/19856832/how-to-use-ondrawcanvas-to-obtain-a-bitmap-snapshot-of-the-webviewandroid">How to use onDraw(Canvas) to obtain [...]</a></li>
  <li><a href="https://stackoverflow.com/questions/6525311/convert-html-to-image-any-format-on-android">Convert HTML to image (any format) on Android</a></li>
  <li><a href="https://stackoverflow.com/questions/15351298/capturing-android-webview-image-and-saving-to-png-jpeg">Capturing android webview image and saving to png/jpeg</a></li>
  <li><a href="https://stackoverflow.com/questions/20942623/which-can-replace-capturepicture-function">Which can replace capturePicture function</a></li>
  <li><a href="https://stackoverflow.com/questions/4538429/capture-picture-from-webview">Capture picture from WebView</a></li>
  </ul>
  
  <p>Ultimately, <em>onPageFinished</em> is not enough and we must implement :</p>
  
  <pre><code>webView.setPictureListener(new PictureListener()
  {
      @Override
      public void onNewPicture(WebView view, Picture picture)
      {
          [...]
  </code></pre>
  
  <p>It works, but it's <em>deprecated</em> and the <em>WebView</em> must be visible if I want to generate a png, and it does not suit me.</p>
  
  <p><strong>Using Android Layout</strong></p>
  
  <p>Some topics about this :</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4188737/how-do-i-convert-a-relativelayout-with-an-imageview-and-textview-to-a-png-image">How do I convert a RelativeLayout [...]</a></li>
  <li><a href="https://stackoverflow.com/questions/2801116/converting-a-view-to-bitmap-without-displaying-it-in-android">Converting a view to Bitmap without displaying it in Android?</a></li>
  <li><a href="https://stackoverflow.com/questions/4960753/android-problems-converting-viewgroup-with-children-into-bitmap">Android Problems Converting ViewGroup with Children into Bitmap</a></li>
  <li><a href="https://stackoverflow.com/questions/4264990/convert-view-into-bitmap">convert view into bitmap [duplicate]</a></li>
  </ul>
  
  <p>Once again, I must have a <em>visible</em> View to generate a PNG from it.</p>
  
  <p><strong>Using <a href="http://code.google.com/p/svg-android/wiki/Tutorial" rel="nofollow noreferrer">svg-android</a></strong></p>
  
  <p>Some topics about this :</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/7201542/svg-to-bitmap-at-runtime-conversion-in-android">SVG to Bitmap at runtime Conversion in Android</a></li>
  </ul>
  
  <p>It works, but there are only basic functions : <em>include text</em>, <em>image</em> or <em>data-uri image</em> <strong>don't work</strong>.</p>
  
  <p><strong>Using <a href="http://xmlgraphics.apache.org/batik/" rel="nofollow noreferrer">Batik</a></strong></p>
  
  <p><a href="https://xmlgraphics.apache.org/batik/tools/rasterizer.html" rel="nofollow noreferrer">SVG Rasterizer</a> and <a href="http://massapi.com/class/pn/PNGTranscoder.html" rel="nofollow noreferrer">its use</a>.</p>
  
  <p>It works, but not on Android.</p>
  
  <blockquote>
    <p>Conversion to Dalvik format failed with error 1</p>
  </blockquote>