I think the title pretty much covers it, but I have a webview in my activity. I've loaded a url into the webview and I'd like to take a screenshot of the full page (whatever is in the viewport and the stuff "below the fold" as well).
I've got code that works to snapshot the viewport, and I know this can be done in iOS by enlarging the webview before snapshotting it. I've tried to use the same technique here:
WebView browserView = (WebView) findViewById(R.id.browserView);
//Resize the webview to the height of the webpage
int pageHeight = browserView.getContentHeight();
LayoutParams browserParams = browserView.getLayoutParams();
browserView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));
//Capture the webview as a bitmap
browserView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(browserView.getDrawingCache());
browserView.setDrawingCacheEnabled(false);
//Create the filename to use
String randomFilenamepart = String.valueOf(new SecureRandom().nextInt(1000000));
String filename = Environment.getExternalStorageDirectory().toString() + "/Screenshot_" + randomFilenamepart + ".jpg";
File imageFile = new File(filename);
//Stream the file out to external storage as a JPEG
OutputStream fout = null;
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
browserView.setLayoutParams(browserParams);
}
But I'm still only capturing just the viewport. Disregarding things like running out of memory because the page is too large, does anyone know what I'm doing wrong or how I can include the portion outside the viewport?
@avinash thakur codes actually works, you just forget to mention to add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
at Android Manifest file, otherwise, android wont allow your stream to write data / file on device.cheers.
Try this one
Add INTERNET PERMISSION and WRITE_EXTERNAL_STORAGE in AndroidManifest.xml file.
Need to ask permission at run time for file write if app is running on or above Marshmallow.