Skip to content Skip to sidebar Skip to footer

Android: Take Screenshot Of Whole Webview In Nougat ( Android 7 )

I use below code to take screenshot from a WebView. it works well in Android 6 and lower versions but in Android 7 it takes only the visible part of the webview. // before setCon

Solution 1:

Use the following methods instead of getMeasuredWidth() & getContentHeight() method:

computeHorizontalScrollRange(); -> for widthcomputeVerticalScrollRange(); -> for height

these two methods will return the entire scrollable width/height rather than the actual widht/height of the webview on screen

To achieve this you need to make WebViews getMeasuredWidth() & getContentHeight() methods public like below

publicclassMyWebViewextendsWebView
{
    publicMyWebView(Context context)
    {
        super(context);
    }

    publicMyWebView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    publicMyWebView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @OverridepublicintcomputeVerticalScrollRange()
    {
        returnsuper.computeVerticalScrollRange();
    }

}

in other work around you can also use a view tree observer to calculate the webview height as shown in this answer

Solution 2:

Remove the below code block

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        WebView.enableSlowWholeDocumentDraw();
    }

Disabling the entire HTML document has a significant performance cost.

As per the documentation,

For apps targeting the L release, WebView has a new default behaviour that reduces memory footprint and increases performance by intelligently choosing the portion of the HTML document that needs to be drawn.

Might be worth looking into this

Answer for Edit 1: To retrieve the webview height after rendering https://stackoverflow.com/a/22878558/2700586

Post a Comment for "Android: Take Screenshot Of Whole Webview In Nougat ( Android 7 )"