Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android webview takes screenshot only from top of the page

I am doing a very simple task of taking a screenshot from my webView into a Bitmap... I do it like this:

webView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(webView.getDrawingCache(false));
webView.setDrawingCacheEnabled(false);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);

The problem is that the final bitmap is always showing the top of my web page content! I actually need it to take the screenshot from where I have scrolled the content but this is not simply happening! I have been searching the net for two hours now but no one else seems to have a similar problem. any idea what could have gone wrong?

like image 653
Hadi tavakoli Avatar asked Jul 08 '15 14:07

Hadi tavakoli


People also ask

How do you screenshot a whole page on Android?

Use the usual button combination (Power + Volume down) to capture a screenshot. On the bottom left corner, you'll see a thumbnail of your screenshot with a button below it called Long Screenshot. Tap on it. Scroll down on the page till you reach your desired point and hit Save.

How can I capture an entire Web page on my phone?

On Android, start by tapping the 'V' icon at the top of the screen and select “Capture page”. Then choose whether you want to take a full page screenshot, or just capture the “Visible Area” (which is, a screenshot of what you can currently see on your screen). The image will be saved to your device.

How to enable JavaScript in WebView in Android?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.


2 Answers

I guess, this is on Lollipop? If so, make sure you call WebView.enableSlowWholeDocumentDraw() (doc) before your first call to setContentView() that inflates a layout with WebView.

like image 163
Mikhail Naganov Avatar answered Oct 17 '22 19:10

Mikhail Naganov


Actually, you don't need the last 2 lines.Just drop them.

webView.setDrawingCacheEnabled(true);  
Bitmap bitmap = Bitmap.createBitmap(webView.getDrawingCache(false));  
webView.setDrawingCacheEnabled(false);

will return the very bitmap you need, from what I've seen.

like image 27
VSim Avatar answered Oct 17 '22 19:10

VSim