1

I have a code to take snapshot of WebView and it works perfectly on pre-L versions:

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    float changeSizeCoefficient = calculateChangeCoef(displayMetrics);
    int sizeTo = (int) (238f * displayMetrics.density * changeSizeCoefficient);

    int height = webView.getWidth() > webView.getContentHeight() ? webView.getContentHeight() : webView.getWidth();
    Bitmap snapshot = Bitmap.createBitmap(webView.getWidth(), height, Bitmap.Config.ARGB_8888);

    Canvas bitmapCanvas = new Canvas(snapshot);
    webView.draw(bitmapCanvas);

    Bitmap resizedSnapshot = Bitmap.createScaledBitmap(snapshot, sizeTo, sizeTo, false);

    if (!resizedSnapshot.equals(snapshot))
        snapshot.recycle();

    return resizedSnapshot;

But on 5.0+ versions the invisible part of WebView on snapshot is filled with white color.

How can i take snapshot of all WebView on Android 5.0+?

Ufkoku
  • 2,384
  • 20
  • 44
  • 1
    Please see http://stackoverflow.com/questions/30078157/webview-draw-not-properly-working-on-latest-android-system-webview-update/30084485 – Mikhail Naganov May 11 '15 at 18:30
  • @Ufkoku Can you please put your code for calculateChangeCoef so, it can help others as full solution for the question? – NovusMobile Jun 20 '17 at 05:40
  • @NovusMobile I asked this question 2+ years ago, and at the moment I can't access this code. I guess you can use an answer above, suggested by Mikhail Naganov, or an answer below, by sahulab. – Ufkoku Jun 20 '17 at 09:53

1 Answers1

0

This implementation of taking snapshot of WebView will work on any Android device,

->Put the WebView in a FrameLayout

<FrameLayout
    android:id="@+id/fl_web_webview_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
       <WebView
           android:id="@+id/wv_webview_activity"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           />
</FrameLayout>

-After that declare and fetch the resource id for both FrameLayout and WebView

Inside your Activity write

WebView mWebView = (WebView) findViewById(R.id.wv_webview_activity);
FrameLayout  fl_web_webview_activity = (FrameLayout)findViewById(R.id.fl_web_webview_activity);

After that put this method in Activity Class

public static Bitmap loadBitmapFromView(View v, int width, int height) {
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);
    return b;
}

Now call above method to get the Bitmap data of your WebView

Bitmap b = loadBitmapFromView(fl_web_webview_activity, fl_web_webview_activity.getWidth(), fl_web_webview_activity.getHeight());

If you use mWebView instead of fl_web_webview_activity it is working fine in below Android 5.0 but in Android 5.0 or above it returning a BLACK or WHITE bitmap.

This implementation is perfectly working on every device below Android 5.0 as well as above Android 5.0.

Hope this implementation will help someone.

Thank You,

sahu
  • 1,188
  • 10
  • 19
  • Thx for an answer, but suggested code takes snapshot of only visible part of WebView. My code takes the snapshot of webview content ,which is out of screen bounds. The answer on link in comment, under my question, is better in my case. http://stackoverflow.com/questions/30078157/webview-draw-not-properly-working-on-latest-android-system-webview-update/30084485 – Ufkoku Aug 28 '15 at 13:06
  • @Ufkoku But I tested in Android 4.4,4.4.4,5.0,4.0 in all its taking snapshot of whole webview,so can I know your Mobile OS details? – sahu Aug 31 '15 at 05:54