Reputation: 1317
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?
Upvotes: 6
Views: 4254
Reputation: 161
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.
Upvotes: 1
Reputation: 561
This worked for me...
First, add webview inside a LinearLayout..
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutWeb"
>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>
</LinearLayout>
Second, initialize views inside onCreate() method...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
webLayout = (LinearLayout) findViewById(R.id.layoutWeb);
....
}
Third, write a function to take a screenshot... Here, View v1 = webLayout; specifies the view which we want to take a screenshot of.
public void takeScreenshot() {
try{
View v1 = webLayout;
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
String mPath = Environment.getExternalStorageDirectory().toString() + "/screenshot.jpg";
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch(Throwable e) {
e.printStackTrace();
}
This takes a screenshot of the part that is covered by webLayout. Scroll up down to focus on other areas and take screenshot of the webView.
Upvotes: 0
Reputation: 547
I came across the same problem and solved it the follwoing way:
First call WebView.enableSlowWholeDocumentDraw()
before setContentView()
. Then take the screenshot over the whole content of the webview and then crop the bitmap to the actual view size with the desired scroll offset:
// the minimum bitmap height needs to be the view height
int bitmapHeight = (webView.getMeasuredHeight() < webView.getContentHeight())
? webView.getContentHeight() : webView.getMeasuredHeight();
Bitmap bitmap = Bitmap.createBitmap(
webView.getMeasuredWidth(), bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
// prevent IllegalArgumentException for createBitmap:
// y + height must be <= bitmap.height()
int scrollOffset = (scrollTo + webView.getMeasuredHeight() > bitmap.getHeight())
? bitmap.getHeight() : scrollTo;
Bitmap resized = Bitmap.createBitmap(
bitmap, 0, scrollOffset, bitmap.getWidth(), webView.getMeasuredHeight());
Upvotes: 0
Reputation: 6861
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.
Upvotes: 3