Android无法从WebView捕获图片,view.postDelay未运行

时间:2017-09-29 21:28:21

标签: java android html webview bitmap

我需要从我的WebView做截图,或者你可以说,将html转换为Bitmap。

我在完成这项任务时遇到了麻烦。 view.postDelayd(new Runnable(){...不要创建一个新的Thread,我的捕获图片在哪里

private void Main {
    WebView webView = new WebView(this.context); //init WebView
    saveHtmlImage(webView);  //running method for capture picture
}

private void saveHtmlImage(final WebView webView){
    webView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            view.postDelayed(new Runnable() {  //trouble here
                @Override
                public void run() {
                    Picture picture = webView.capturePicture();
                    Bitmap b = Bitmap.createBitmap( picture.getWidth(),
                            picture.getHeight(), Bitmap.Config.ARGB_8888);
                    Canvas c = new Canvas(b);
                    picture.draw(c);
                }
            }, 100);
        }
    });

    //set HTML code for WebView
    webView.loadDataWithBaseURL(null, "<TABLE border=\"1\"\n" +
            "          summary=\"This table gives some statistics about fruit\n" +
            "                   flies: average height and weight, and percentage\n" +
            "                   with red eyes (for both males and females).\">\n" +
            "<CAPTION><EM>A test table with merged cells</EM></CAPTION>\n" +
            "<TR><TH rowspan=\"2\"><TH colspan=\"2\">Average\n" +
            "    <TH rowspan=\"2\">Red<BR>eyes\n" +
            "<TR><TH>height<TH>weight\n" +
            "<TR><TH>Males<TD>1.9<TD>0.003<TD>40%\n" +
            "<TR><TH>Females<TD>1.7<TD>0.002<TD>43%\n" +
            "</TABLE>", "text/html", "UTF-8", null);
}

当我不使用Runnable时,Bitmap.createBitmap(...)给出异常 - java.lang.IllegalArgumentException:width和height必须是&gt; 0

当我使用

Handler handler = new Handler();
handler.postDelayed(new Runnable() {

intead

view.postDelayd(new Runnable() {

方法

webView.capturePicture();

返回空截图,但宽度和高度正确

请帮帮我。

说出我做错了什么,或者给我另一个可以将html代码转换为Bitmap Image的代码示例。

提前感谢您的回复。

1 个答案:

答案 0 :(得分:0)

只需一个力量来适应全屏并编码标准屏幕截图并转换为位图。如果您需要一个例子,请告诉我。