我需要从HTML代码中拍照。 (将HTML转换为位图)。
对于此任务,我正在使用此代码,除了API 23之外,它适用于大多数apis
WebView webView = new WebView(context);
...
void saveHtmlImage(final PrintData data) {
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(final WebView view, String url) {
//view.postDelayed(new Runnable() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Picture picture = webView.capturePicture();
Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
picture.draw(c);
data.setBitmap(bitmap);
callback.callingBack(data);
}
}, 500);
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache(true);
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);
}
Capture Picture正在运作
捕获图片不起作用