Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

html - 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 div in the page flip html. I noticed the Picture class in android's docs but I'm not sure if that can be converted and saved. Could this be done through JS? Any ideas?

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

webview.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {
        Picture picture = view.capturePicture();
        Bitmap b = Bitmap.createBitmap(
            picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        picture.draw(c);

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream( "/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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...