Skip to content Skip to sidebar Skip to footer

Capturing Android Webview Image And Saving To Png/jpeg

I'm trying to implement the css3 page flip effect on a android/phonegap app. To do this, I need to dynamically save the current webview to png or jpeg so that it can be loaded to a

Solution 1:

Try following code for capturing webview and saved jpg to sdcard.

webview.setWebViewClient(newWebViewClient() {

    @OverridepublicvoidonPageFinished(WebView view, String url) {
        Picturepicture= view.capturePicture();
        Bitmapb= Bitmap.createBitmap(
            picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
        Canvasc=newCanvas(b);
        picture.draw(c);

        FileOutputStreamfos=null;
        try {
            fos = newFileOutputStream( "/sdcard/"  + "page.jpg" );
            if ( fos != null ) {
                b.compress(Bitmap.CompressFormat.JPEG, 90, fos );
                fos.close();
            }
        } 
        catch( Exception e ) {
            System.out.println("-----error--"+e);
        }
    }
});

webview.loadUrl("http://stackoverflow.com/questions/15351298/capturing-android-webview-image-and-saving-to-png-jpeg");

Solution 2:

The easiest way (to my knowledge) to capture a View as an image is to create a new Bitmap and a new Canvas. Then, simply ask your WebView to draw itself on your own Canvas instead of the Activity's default one.

Pseudocode:

Bitmapbitmap= Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(bitmap);
myWebView.draw(canvas);
//save your bitmap, do whatever you need

Post a Comment for "Capturing Android Webview Image And Saving To Png/jpeg"