我想从 webview 整页生成位图。
一些代码
下面是从 webview 生成 Bitmap 的代码
网页 View 到位图:
Picture p = webview.capturePicture();
webview.setDrawingCacheEnabled(true);
webview.buildDrawingCache();
final PictureDrawable pictureDrawable = new PictureDrawable(pict);
Bitmap bitmap = null;
try
{
bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
}
catch (OutOfMemoryError e)
{
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
webview.setDrawingCacheEnabled(false);
下面的代码用于将文件保存在内存中
保存为文件:
if(bitmap!=null)
{
File myDir = new File(Environment.getExternalStorageDirectory(), "AackAack");
if (myDir.exists())
{
}
else
{
myDir.mkdir();
}
String fname = System.currentTimeMillis() + ".png";
file1 = new File(myDir, fname);
try
{
FileOutputStream out = new FileOutputStream(file1);
result.compress(Bitmap.CompressFormat.PNG, 10, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
此处 webview 加载正常但未完全在 Lollipop 中生成位图。 这个问题只出现在 Lollipop 设备中。和相同的代码在 kitkat 上运行然后它工作正常..
我该如何解决这个问题?请帮助我..
提前致谢..
最佳答案
您需要在创建任何 WebView 之前调用 WebView.enableSlowWholeDocumentDraw()。也就是说,如果您的布局中有任何 WebView,请确保在调用 onCreate() 中的 setContentView() 之前调用此方法,如下所示。
if (Build.VERSION.SDK_INT >= 21) {
webview.enableSlowWholeDocumentDraw ();
}
它对我来说效果很好..
谢谢
关于android - webview 在 lollipop android 中没有完全生成位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393558/