绘制屏幕外的WebView为位图 [英] Drawing offscreen WebView to bitmap

查看:127
本文介绍了绘制屏幕外的WebView为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉排出的屏幕在Android的用户带来的WebView的形象,我总是最后一个黑色的图像。这是正确的大小,一切都没有了。

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 size and everything just not the

下面是code我使用的:

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.

推荐答案

webview.loadUrl(theURL); 启动页面的加载,这需要一段时间。等待你的画web视图在页面加载后,直到前。您可以使用 setWebViewClient(WebViewClient客户端)来建立一个对​​象,以接收通知,当网页加载完毕。

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.

这篇关于绘制屏幕外的WebView为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆