jcrowson
jcrowson

Reputation: 4290

Find the WebView that Phonegap uses?

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

Upvotes: 3

Views: 3051

Answers (2)

Chad Schultz
Chad Schultz

Reputation: 7860

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.

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006584

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:

Upvotes: 1

Related Questions