我正在尝试从webview中的网页生成位图。 我只能获得可见屏幕的位图,并且无法获得屏幕其余部分的位图。 所以,我手动向下滚动屏幕再次取位图,但此时生成的位图是空白的。
这是我正在使用的代码:
try {
Bitmap bitmap = Bitmap.createBitmap(webview.getWidth(), webview.getHeight(), Bitmap.Config.ARGB_8888);
// Bitmap bitmap = Bitmap.createBitmap(webview.getDrawingCache());
Canvas canvas = new Canvas(bitmap);
webview.draw(canvas);
String GVCode = String.valueOf(System.currentTimeMillis());
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/WebToPDF");
myDir.mkdirs();
String fname = GVCode + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Document document = new Document();
String input = Environment.getExternalStorageDirectory().toString() + "/WebToPDF/" + GVCode + ".jpg";
String output = Environment.getExternalStorageDirectory().toString() + "/WebToPDF/" + GVCode + ".pdf";
String stringPrintDirectory = Environment.getExternalStorageDirectory().toString() + "/WebToPDF/";
File myDirPdf = new File(stringPrintDirectory);
if (!myDirPdf.exists()) {
myDirPdf.mkdirs();
}
FileOutputStream fos = new FileOutputStream(output);
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.open();
document.open();
Image img = Image.getInstance(input);
document.setPageSize(img);
document.newPage();
img.setAbsolutePosition(0, 0);
document.add(img);
document.close();
writer.close();
LogShowHide.LogShowHideMethod("+++ : ", "Bitmap is Done.");
webview.scrollTo(0, height);
height = height + height;
} catch (Exception e) {
e.printStackTrace();
}
如果有人能弄清楚我在哪里犯错误,那将会帮助我很多......
如果需要,请随时提出更多信息。