Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture picture from android webview

Tags:

android

I want to save the picture getting from capturePicture method in WebView how to do this?

like image 509
Prasanth Avatar asked Oct 09 '11 09:10

Prasanth


1 Answers

web = new WebView(this);

web.setPictureListener(new PictureListener(){

    public void onNewPicture(WebView view, Picture picture) {
      if(picture != null)
      {
       try
       {
        Bitmap bmp = pictureDrawable2Bitmap(new PictureDrawable(picture)); 
        FileOutputStream out = new FileOutputStream(filename);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
        out.close();
        }
        catch(Exception e)
        {
           e.printStackTrace();
        }
      }
});

web.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView webview, String url) {
        Picture picture = webview.capturePicture();

    }
});


web.getSettings().setJavaScriptEnabled(true);

setContentView(web);

helper function

private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
        Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth()
        ,pictureDrawable.getIntrinsicHeight()
        , Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(pictureDrawable.getPicture());
        return bitmap;
    }

Disclaimer: I haven't actually compiled this, it should work though

like image 106
Reno Avatar answered Sep 22 '22 18:09

Reno