tags:

views:

788

answers:

1

[Android Newbie alert]

I need to capture the contents of a WebView in a BitMap and I've run into a strange problem. My approach is to register a WebViewClient with the 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 a Picture with height and width values = 0. The page loads fine, either way. The actual url I have to use has quite a few url parameters and I initially thought having any parameters was the problem, but that's not the case. Here's a few sample urls with comments indicating whether it works or not:

  1. w.loadUrl("http://www.yahoo.com"); //yes
  2. w.loadUrl("http://search.yahoo.com/search?p=android"); // usually not???
  3. w.loadUrl("http://www.yahoo.com?foo=bar"); // nope
  4. w.loadUrl("http://www.google.com"); // yep
  5. w.loadUrl("http://www.google.com?q=android"); // yep
  6. w.loadUrl("http://www.google.com?foo=bar"); // yes

The second case is particularly frustrating as it appears to not work. However, if I run the test app with #5 first, then switching the url to #2 and running it then works.

Here's a snippet of an actual simplified test 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 run into this issue? Hopefully I'm just being an idiot and there's a simple solution or workaround?

Thanks!

+3  A: 

I just visited the documentation page again. [here][1]

"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)."

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

I just added below code 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%28android.webkit.WebView, java.lang.String)

bhatt4982
Thanks so much for your help. However, I'm unable to get onNewPicture to be triggered. From the API description, I would think that I still have to listen for onPageFinished to begin the capturePicture and then use onNewPicture to listen for that to complete, but I must not we setting up the listener correctly. If I'm supposed to use onNewPicture instead of onPageFinished, I still don't ever see it getting called. Could you provide a bit more detail on how you got it to work?
Ah, I was making things much too complicated by not realizing that the Picture comes "free". Thanks for your help.