[Android Newbie alert]
I need to capture the contents of a WebView in a BitMap and I've run into a strange problem. My approach is to register a WebViewClient with the WebView, and in onPageFinished I call capturePicture. With a simple URL (e.g. http://www.yahoo.com), it works fine. In other cases, capturePicture returns a Picture with height and width values = 0. The page loads fine, either way. The actual url I have to use has quite a few url parameters and I initially thought having any parameters was the problem, but that's not the case. Here's a few sample urls with comments indicating whether it works or not:
- w.loadUrl("http://www.yahoo.com"); //yes
- w.loadUrl("http://search.yahoo.com/search?p=android"); // usually not???
- w.loadUrl("http://www.yahoo.com?foo=bar"); // nope
- w.loadUrl("http://www.google.com"); // yep
- w.loadUrl("http://www.google.com?q=android"); // yep
- w.loadUrl("http://www.google.com?foo=bar"); // yes
The second case is particularly frustrating as it appears to not work. However, if I run the test app with #5 first, then switching the url to #2 and running it then works.
Here's a snippet of an actual simplified test I created:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
w = new WebView(this);
w.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView webview, String url) {
Picture picture = webview.capturePicture();
Log.d("Height", "" + picture.getHeight());
Log.d("Width", "" + picture.getWidth());
Bitmap b = Bitmap.createBitmap(picture.getWidth(), picture
.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);
}
});
w.getSettings().setJavaScriptEnabled(true);
setContentView(w);
//w.loadUrl("http://www.yahoo.com"); //yes
w.loadUrl("http://search.yahoo.com/search?p=android"); // usually not???
//w.loadUrl("http://www.yahoo.com?foo=bar"); // nope
//w.loadUrl("http://www.google.com"); // yep
//w.loadUrl("http://www.google.com?q=android"); // yep
//w.loadUrl("http://www.google.com?foo=bar"); // yes
}
Has anyone run into this issue? Hopefully I'm just being an idiot and there's a simple solution or workaround?
Thanks!