1

here i have a quick question on webview.

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

Below code for generating Bitmap from webview

Webview to Bitmap :

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);

below code for to save the file in memory for that

To save as file :

 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();
    }}

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

enter image description here

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

Thanks in advance..

user512
  • 403
  • 4
  • 21
  • 1
    dont measure the webview instead capture the layout in which the webview exist and convert it into a bitmap. – DJphy May 23 '15 at 07:43

2 Answers2

1

U can draw the view on a canvas like this:

        Bitmap mBitmap;
        Layout webViewContainer
        mBitmap =  Bitmap.createBitmap(webViewContainer.getWidth(), webViewContainer.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mBitmap);
        webViewContainer.draw(canvas);
DJphy
  • 1,292
  • 1
  • 17
  • 31
  • thanks for reply... here getting webviewcontainer as null. do u provide some snippet – user512 May 23 '15 at 08:55
  • Please check this url http://stackoverflow.com/questions/9161192/webviewfragment-webview-is-null-after-doing-a-fragmenttransaction – syam vakkalanka May 23 '15 at 09:34
  • Bro its ur webViewContainer(the layout in which ur webview is present..it can be ur linear or relative layout according to ur setup), layout is just a general ViewGroup term i have taken here. – DJphy May 23 '15 at 10:18
  • ok.. thanks for ur reply.. but i found the solution for this... any way thanks again... – user512 May 23 '15 at 10:43
1

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..

user512
  • 403
  • 4
  • 21