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

问题描述 投票:-2回答:2

这是我的代码

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的一部分工作成功但保存图像不工作请帮助我,我需要这个选项。

android webview imageview
2个回答
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
投票

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

webView.setDrawingCacheEnabled(true);

在onPageFinished()中

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

希望这可以帮助...

© www.soinside.com 2019 - 2024. All rights reserved.