开发者

Capture picture from android webview

I wan开发者_Go百科t to save the picture getting from capturePicture method in WebView how to do this?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜