在屏幕外绘制WebView到位图上

5
我正在尝试在Android中捕获绘制在用户屏幕外的Webview图像,但最终得到的都是黑色图像。它的大小和一切都是正确的,只是缺少了某些东西。

这是我使用的代码:

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();
        }       

感谢任何能够提供帮助的人。

1个回答

0

webview.loadUrl(theURL); 开始加载页面,这可能需要一些时间。在页面加载完成之前,请等待再绘制您的 webview。您可以使用 setWebViewClient(WebViewClient client) 来设置一个对象,以接收页面加载完成的通知。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接