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.