3

I've yet to find an answer for this and it seems like such a simple task.

I need to be able to find the WebView that Phonegap uses when it calls:

    super.loadUrl("file:///android_asset/www/index.html");

I've tried creating a new WebView in the main.xml file and loading the index.html page into it which only causes a Null Pointer Exception.

Any help would be great.

///

Edit:

The code below appears to now actually take a screenshot of the view as the file has a size of 823bytes..however, it is displaying as just a black image, nothing is shown..

Could this be to it taking a screenshot too quickly, before the view is loaded? or am I doing something else wrong?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/index.html");


    View html = (View)super.appView.getParent(); 
    Bitmap bm2 = Bitmap.createBitmap(200, 300, Bitmap.Config.ARGB_8888);
    Canvas c2 = new Canvas(bm2);
    html.draw(c2);

    OutputStream stream = null;
    try {
      stream = new FileOutputStream(Environment.getExternalStorageDirectory() +"/bm2.png");
      bm2.compress(CompressFormat.PNG, 80, stream);
      if (stream != null) stream.close();
    } catch (IOException e) {
    } finally {
      bm2.recycle();
    }

}//end
jcrowson
  • 4,290
  • 12
  • 54
  • 77
  • 1
    Run your app in the emulator and use Hierarchy View to try to track down the `R.id` value for the `WebView`. It should be the only `WebView` in your layout, AFAIK. – CommonsWare Nov 20 '11 at 23:25
  • @CommonsWare Thanks for your reply, I've updated my original post with some insights! – jcrowson Nov 20 '11 at 23:46

2 Answers2

3

I had the same question--how do I get a reference to the WebView - so I will post my solution for posterity (and so I can find it again in the future).

Looking at https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaActivity.java, the WebView used appears to be the protected variable appView, which is accessible from your main Android class. So just use the appView variable.

Chad Schultz
  • 7,770
  • 6
  • 57
  • 96
1

If I had to guess, WebView doesn't honor the drawing cache.

Try using capturePicture(), then rendering that Picture to a Bitmap-backed Canvas, and saving the resulting bitmap. See:

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Excellent reply CommonsWare, I'm now able to take a screenshot, however it is just appearing as black? There is an update in my original post. – jcrowson Nov 21 '11 at 10:04
  • @LordSnoutimus: You will notice that my answer and the linked-to answers refer to using `capturePicture()` and `setPictureListener()`, not `draw()`. – CommonsWare Nov 21 '11 at 13:14
  • 1
    @LordSnoutimus: I suggest that you take your concerns to http://groups.google.com/group/phonegap as that would appear to be a better resource for getting PhoneGap-specific answers. I have not written a PhoneGap plugin yet (might do one in 2012), so I don't know how to pull off what's needed within the plugin confines. – CommonsWare Nov 21 '11 at 15:03