Drawing offscreen WebView to bitmap
I am trying to capture a image of a webview drawn off screen to the user in android and i always end up with a black image. It is the correct s开发者_开发问答ize and everything just not the
Here is the code I am using:
String theURL = "file:///android_asset/www/pages/page2.html";
WebView webview = new WebView(ctx);
webview.loadUrl(theURL);
Bitmap bm = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
webview.draw(c);
OutputStream stream = null;
try {
stream = new FileOutputStream(Environment.getExternalStorageDirectory() +"/page.jpg");
bm.compress(CompressFormat.JPEG, 80, stream);
if (stream != null) stream.close();
return new PluginResult(PluginResult.Status.OK);
} catch (IOException e) {
return new PluginResult(PluginResult.Status.ERROR, e.toString());
} finally {
bm.recycle();
}
Thanks if anyone can help.
The webview.loadUrl(theURL);
starts the loading of the page, that can take a while. Wait before drawing your webview until after the page is loaded. You can use a setWebViewClient(WebViewClient client)
to set up an object to receive notice when the page has finished loading.
精彩评论