【发布时间】:2019-12-29 03:28:23
【问题描述】:
我认为标题几乎涵盖了它,但我的活动中有一个 webview。我已将 url 加载到 webview 中,我想截取整个页面的屏幕截图(视口中的任何内容以及“首屏下方”的内容)。
我有用于快照视口的代码,我知道这可以在 iOS 中通过在创建快照之前放大 webview 来完成。我在这里尝试使用相同的技术:
WebView browserView = (WebView) findViewById(R.id.browserView);
//Resize the webview to the height of the webpage
int pageHeight = browserView.getContentHeight();
LayoutParams browserParams = browserView.getLayoutParams();
browserView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));
//Capture the webview as a bitmap
browserView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(browserView.getDrawingCache());
browserView.setDrawingCacheEnabled(false);
//Create the filename to use
String randomFilenamepart = String.valueOf(new SecureRandom().nextInt(1000000));
String filename = Environment.getExternalStorageDirectory().toString() + "/Screenshot_" + randomFilenamepart + ".jpg";
File imageFile = new File(filename);
//Stream the file out to external storage as a JPEG
OutputStream fout = null;
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
browserView.setLayoutParams(browserParams);
}
但我仍然只捕获视口。忽略诸如由于页面太大而耗尽内存之类的事情,有谁知道我做错了什么或如何将部分包含在视口之外?
【问题讨论】:
-
我不确定这是不是重复的。那里描述的方法有一个大问题,不推荐使用 PictureListener.onNewPicture() 。根据文档 - “由于内部变化,图片不包括复合层,例如固定位置元素或可滚动 div。虽然 PictureListener API 仍可用于检测 WebView 内容的变化,但建议您不要使用它,直到在未来的 Android 版本中提供了替代品”。这听起来不像是我要获取页面上的所有元素。
标签: android webview screenshot