Pergunta

I was trying to implement this feature in Apache Cordova PhoneGap, but it doesn't work for me

How to programmatically take a screenshot in Android?

How can I programmatically take a screenshot of a webview, capturing the full page?

When I call to

 WebView browserView = (WebView) findViewById(R.id.content);

I am actually getting a reference to the following object

 LinearLayoutSoftKeyboardDetect

and not to the WebView.

Any idea on how to fix it?

Foi útil?

Solução

Cordova/PhoneGap-based Applications encapsulate the WebView into a DroidGap class that ultimately extends from CordovaActivity CordovaActivity. This Activity holds a protected component called containing the wiew CordovaWebView appView and configured with an ID equals to 100:

protected CordovaWebView appView;
...
this.appView.setId(100);

Thus, you should be able to find the View by seeking the ID 100 doing something like this:

CordovaWebView view = (CordovaWebView) activity.findViewById(100);
Picture picture = view.capturePicture();
Bitmap b = Bitmap.createBitmap (picture.getWidth(), 
                                picture.getHeight(), 
                                Bitmap.Config.ARGB_8888);
picture.draw(new Canvas(b));
FileOutputStream foss = new FileOutputStream(out);
b.compress(Bitmap.CompressFormat.PNG, 100, foss);
foss.close(); 

This should work.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top