我如何将webview内容转换为图像文件?

时间:2017-12-25 12:27:53

标签: android webview imageview

这是我的代码

btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
                createWebPrintJob(webView);
            }else {


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


                Toast.makeText(WebViewActivity.this, "Saved", Toast.LENGTH_SHORT).show();
            }

        }
    });

如果用户sdk> kitkat然后webview被视为pdf并且如果用户sdk

保存pdf的一部分工作成功,但保存图像不起作用 请帮帮我,我需要这个选项。

2 个答案:

答案 0 :(得分:0)

我在我的一个项目中使用过这段代码。这很好用。试一试。

   webView.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                // do your stuff here
                webView.measure(MeasureSpec.makeMeasureSpec(
                        MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),
                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                webView.layout(0, 0, webView.getMeasuredWidth(),
                        webView.getMeasuredHeight());
                webView.setDrawingCacheEnabled(true);
                webView.buildDrawingCache();
                Bitmap bm = Bitmap.createBitmap(webView.getMeasuredWidth(),
                        webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

                Canvas bigcanvas = new Canvas(bm);
                Paint paint = new Paint();
                int iHeight = bm.getHeight();
                bigcanvas.drawBitmap(bm, 0, iHeight, paint);
                webView.draw(bigcanvas);
                System.out.println("WIDTH"
                        + bigcanvas.getWidth());
                System.out.println("HEIGHT="
                        + bigcanvas.getHeight());

                if (bm != null) {
                    try {
                        String path = Environment.getExternalStorageDirectory()
                                .toString();
                        OutputStream fOut = null;
                        File file = new File(path, "/aaaa.png");
                        fOut = new FileOutputStream(file);

                        bm.compress(Bitmap.CompressFormat.PNG, 50, fOut);
                        fOut.flush();
                        fOut.close();
                        bm.recycle();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

答案 1 :(得分:-1)

很久以前我曾经这样做过。

webView.setDrawingCacheEnabled(true);

在onPageFinished()

Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
imageView.setImageBitmap(bitmap);

希望这会有所帮助......