I'm trying to get the whole screen shot programmatically in webview. When I test my code, it returns a white screen.
I've tried different codes, but I couldn't get it done. Sometimes it returns a white screen, sometimes it returns a black screen. I've also added my manifest Internet permission and wrote an external storage permission. My xml file exist only in webview.
I've also tried "sleep" for five seconds but it didn't work.
public static Bitmap loadBitmapFromView(WebView v, int width, int height)
{
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
FileOutputStream fos = null;
try
{
fos = new FileOutputStream("/sdcard/image.jpg" );
}
catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
b.compress(Bitmap.CompressFormat.JPEG, 90, fos);
try
{
fos.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
I call this method in onpageFinished()
. By loadBitmapFromView(webView, webView.getWidth(), webView.getHeight());
I ensure loading the whole page. In my opinion, it tries to get a screen shot when webview doesn't load before the call. But I call it in the onpageFinished()
method. It runs when webview loads completely.
Other parts of my code
public class Wv extends Activity
{
private static WebView webView;
CustomWebViewClient webViewClient;
ProgressDialog mProgressDialog;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");
webView.loadUrl("file:///sdcard/ZipDemo/HTML_Friday.html");
webViewClient = new CustomWebViewClient();
webView.setWebViewClient(webViewClient);
}
private class CustomWebViewClient extends WebViewClient
{
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!mProgressDialog.isShowing())
{
mProgressDialog.show();
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
loadBitmapFromView(webView,webView.getWidth(),webView.getHeight());
}
}