【问题标题】:how i can convert webview content to image file?我如何将 webview 内容转换为图像文件?
【发布时间】:2017-12-25 12:27:53
【问题描述】:

这是我的代码

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

        }
    });

if users sdk>kitkat then webview seved as pdf and if users sdk

部分保存 pdf 成功,但保存图像无效 请帮帮我,我需要这个选项。

【问题讨论】:

标签: android webview imageview


【解决方案1】:

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

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

【讨论】:

  • 谢谢,我测试了这段代码。我没有错误但不工作。
【解决方案2】:

很多时候我曾经这样做过。

webView.setDrawingCacheEnabled(true);

在 onPageFinished()

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

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 2019-10-06
    • 2013-09-18
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多