WebView.capturePicture error with url parameters

[Android Newbie alert]

I need to grab the contents of a WebView in BitMap, and I am having a strange problem. My approach is to register a WebViewClient with a WebView, and in onPageFinished I call capturePicture. With a simple URL (e.g. http://www.yahoo.com ), it works fine. In other cases, capturePicture returns an image with height and width = 0. The page loads fine, anyway. The actual url that I should use has quite a few url parameters, and initially I thought that having any parameters was a problem, but that is not the case. Here are some examples of URLs with comments indicating whether it works or not:

The second case is especially frustrating because it does not seem to work. However, if I first run the C # 5 test application, then the URL switches to # 2 and launches it.

Here is a snippet of the actual simplified test that I created:

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 
  w = new WebView(this); w.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView webview, String url) { Picture picture = webview.capturePicture(); Log.d("Height", "" + picture.getHeight()); Log.d("Width", "" + picture.getWidth()); Bitmap b = Bitmap.createBitmap(picture.getWidth(), picture .getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); picture.draw(c); } }); w.getSettings().setJavaScriptEnabled(true); setContentView(w); //w.loadUrl("http://www.yahoo.com"); //yes w.loadUrl("http://search.yahoo.com/search?p=android"); // usually not??? //w.loadUrl("http://www.yahoo.com?foo=bar"); // nope //w.loadUrl("http://www.google.com"); // yep //w.loadUrl("http://www.google.com?q=android"); // yep //w.loadUrl("http://www.google.com?foo=bar"); // yes } 

Has anyone encountered this problem? I hope I'm just an idiot and is there a simple solution or a workaround?

+4
source share
1 answer

I just visited the documentation page. [Here] [1]

Msgstr "Notify the host application that the page has finished loading. This method is called only for the main frame. When onPageFinished () is called, the rendering image may not be updated. New Picture, use onNewPicture (WebView, figure) ."

Used Picture Listener, I tried with your sample and it works.
Hope this helps.

I just added the code below to your example and removed WebViewClient

  w.setPictureListener(new PictureListener(){ public void onNewPicture(WebView view, Picture picture) { Log.d(TAG, "onNewPicture- Height"+ picture.getHeight()); Log.d(TAG, "onNewPicture- Width"+ picture.getWidth()); } }); 

[1]: http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished(android.webkit.WebView , java.lang.String)

+8
source

All Articles