如何捕获网页视图中的是Android 5.0为位图? [英] How to capture the webview to bitmap in android 5.0?

查看:143
本文介绍了如何捕获网页视图中的是Android 5.0为位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我对的WebView一个快速的问题。

here i have a quick question on webview.

我的要求是捕捉到的WebView和SD卡的文件保存到我下面用code。

My requirement is capture the webview and save the file in sdcard to that i used below code.

下面code,用来从位图的WebView

Below code for generating Bitmap from webview

网页视图为位图:

webview.measure(MeasureSpec.makeMeasureSpec(
               MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),
               MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
       webview.layout(0, 0, webview.getMeasuredWidth(),
               webview.getMeasuredHeight());
       webview.setDrawingCacheEnabled(true);
       webview.buildDrawingCache();
      bitmap = Bitmap.createBitmap(webview.getMeasuredWidth(),
               webview.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

       Canvas bigcanvas = new Canvas(bitmap);
       Paint paint = new Paint();
       int iHeight = bitmap.getHeight();
       bigcanvas.drawBitmap(bitmap, 0, iHeight, paint);
       webview.draw(bigcanvas);

    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }

   webview.setDrawingCacheEnabled(false);

低于code为将文件保存在内存中的

below code for to save the file in memory for that

要保存为文件:

 File myDir = new File(Environment.getExternalStorageDirectory(), "Sample");
    if (myDir.exists()) 
    {
    } 
    else 
    {
        myDir.mkdir();
    }
    String fname = "sample" + ".png";
    file1 = new File(myDir, fname);

   if(bitmap!=null)
   { 

    try 
    {
        FileOutputStream out = new FileOutputStream(file1);
        bitmap.compress(Bitmap.CompressFormat.PNG, 10, out);
        out.flush();
        out.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }}

但这里的WebView加载罚款,如下面的图像不在是Android 5.0(棒棒糖)完全捕获

but here webview loading fine but not capturing completely in android 5.0(lollipop) as shown in below image

我怎样才能解决这个问题呢?请给我建议或code的一些片断。

how can i resolve this issue? please give me suggestions or some snippet of code.

在此先感谢..

推荐答案

您需要创建任何网页视图之前调用WebView.enableSlowWholeDocumentDraw()。也就是说,如果你在你的布局网页视图的任何,请确保你在你的onCreate()所示调用的setContentView()之前调用此方法。

You need to call WebView.enableSlowWholeDocumentDraw() before creating any WebViews. That is, if you have any WebViews in your layout, make sure you call this method before calling setContentView() in your onCreate() shown below.

if (Build.VERSION.SDK_INT >= 21) {
        webview.enableSlowWholeDocumentDraw ();
}

对于我来说它的做工精细。

its working fine for me..

这篇关于如何捕获网页视图中的是Android 5.0为位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆