I'm trying to take a screenshot of a webview in Android. However the screenshot fires too quickly and as a result, I get a blank screenshot. I tried implementing a webviewclient and onPageFinished to listen for the webview to load before taking the shot, but it didn't work. How do I make sure the view loads before taking the screenshot?
public void onSaveClicked(View reLayout){
final WebView webview;
setContentView(R.layout.webview);
webview = (WebView) findViewById(R.id.webview);
WriteJsJson();
Activity context;
context = _activity.get();
Intent fire = new Intent(context, WebviewActivity.class);
switch (_reportType) {
case 1 :
fire.putExtra("target", "daily"); // Parameter to tell the webview activity to open the right report.
case 2 :
fire.putExtra("target", "week");
case 3 :
fire.putExtra("target", "month");
}
startActivity(fire);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
grabScreen(); //method for taking screenshot and storing it...
}
});
You can add setWebChromeClient to see the process of webview.
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, final int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
grabScreen();
}
}
});
onPageFinished notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).
Sample Code
mWebView.setPictureListener(new MyPictureListener());
//... and then later on....
class MyPictureListener implements PictureListener {
@Override
public void onNewPicture(WebView view, Picture arg1) {
// put code here that needs to run when the page has finished loading and
// a new "picture" is on the webview.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With