Drawing offscreen WebView to bitmap. Blank output.

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Drawing offscreen WebView to bitmap. Blank output.

Postby AM » Sun Aug 09, 2009 1:45 am


I need to create a WebView (never shown on screen, only for internal purposes), use it to load a page, and then draw that page to an offscreen bitmap for later use.

I've tried all sorts of things, but having no luck.

I'm creating the WebView with:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. webView = new WebView(context);
Parsed in 0.030 seconds, using GeSHi

I'm then using [font=Courier New]webView.loadUrl(url)[/font] to load the web page I need to draw.

I then attempt to call the method to draw the page to a Bitmap from an attached WebViewClient's [font=Courier New]onPageFinished[/font] method.

The first thing I tried for this was this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         Bitmap bmpBuffer = Bitmap.createBitmap(width, height, Config.ARGB_8888);
  3. Canvas offscreen = new Canvas(bmpBuffer);
  5. webView.draw(offscreen);
Parsed in 0.030 seconds, using GeSHi

No luck. All that is drawn is whatever is the WebView's BackgroundDrawable/BackgroundColor.

I figured maybe the correct size of the WebView might not be set, but not really sure what the right way to go about this I tried adding all sorts of variations on the following before the draw call:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. webView.setLayoutParams(new LayoutParams(width, height));
  3. webView.measure(width, height);
  5. webView.layout(0, 0, width, height);
Parsed in 0.034 seconds, using GeSHi

I also tried [font=Courier New]webView.invalidate()[/font] and [font=Courier New]webView.postInvalidate()[/font] both before and after the draw call. Same result.

I also tried [font=Courier New]webView.capturePicture()[/font], but noticed that the resulting picture would always have a size of 0x0 (checked using [font=Courier New]picture.getWidth()[/font] and [font=Courier New]picture.getHeight();[/font].

I tried calling [font=Courier New]capturePicture()[/font] both in the method I call from onPageFinished as well as indirectly in an overloaded [font=Courier New]onDraw(...)[/font] method for the WebView. In both cases the resulting Picture had a size of 0.

The page IS being loaded properly. I even went so far as to use [font=Courier New]addJavascriptInterface[/font] to call Android's [font=Courier New]Log.d(...)[/font] from a javascript in the page to confirm this. The page also displays properly in the Android web browser.

Any help? What am I doing wrong? :(

Posts: 4
Joined: Thu Jun 18, 2009 2:57 pm


Postby cooolmagic » Thu Aug 20, 2009 4:40 pm

try these steps..

1. Add webview to view hierarchy. Create WebView and add to a hidden ViewGroup (with visibility = INVISIBLE not GONE) in your onCreate().
2. Set webView.setDrawingCacheEnabled(true). Call loadURL.
3. Let first drawing pass happen. You can use a Handler and send 1 sec delayed message at end of onResume().
4. In your handleMessage:
- viewGroup.setBackgroundDrawable(aWhiteDrawable); // imp if page has not loaded anything yet
- Bitmap b = webView.getDrawingCache();
- viewGroup.setBackgroundDrawable(null); // remove background

best of luck.

Once Poster
Once Poster
Posts: 1
Joined: Thu Aug 20, 2009 4:28 pm


Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests