从android webview捕获图片

时间:2011-10-09 09:27:05

标签: android

我想在WebView中保存来自capturePicture方法的图片怎么做?

1 个答案:

答案 0 :(得分:13)

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);

辅助功能

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;
    }

免责声明:我实际上并没有编译它,它应该可以工作