【问题标题】:Capture picture from webview loading local html从加载本地html的webview中捕获图片
【发布时间】:2012-11-21 13:48:48
【问题描述】:

我正在尝试从 webiview 捕获图像,当我尝试从网络加载 URL 时它可以工作,但是当我尝试在资产中加载本地 html 文件或在字符串中加载 html 时,它会崩溃并出现以下错误:

java.lang.IllegalArgumentException: width and height must be > 0
    at android.graphics.Bitmap.createBitmap(Bitmap.java:638)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:620)

我的代码是:

//Create the webview

WebView w = new WebView(this);

w.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        //get the picture from webview
        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 {

        String path = Environment.getExternalStorageDirectory().toString();
        File dir = new File(path, "/Movel/media/img/");
        if (!dir.isDirectory()) {
            dir.mkdirs();
        }
        String arquivo = "darf_"+ System.currentTimeMillis() + ".jpg";
        File file = new File(dir, arquivo);

        fos = new FileOutputStream(file);
        String imagePath =  file.getAbsolutePath();
        //scan the image so show up in album
        MediaScannerConnection.scanFile(MainActivity.this,  new String[] { imagePath }, 
                        null, new MediaScannerConnection.OnScanCompletedListener() {
                            public void onScanCompleted(String path, Uri uri) {

                            }
                            });

        if (fos != null) {
            b.compress(Bitmap.CompressFormat.JPEG, 90, fos);

            fos.close();
        }

        } catch (Exception e) {
        e.printStackTrace();
        }
    }
    });

setContentView(w);

String html = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'> " +
    "<title>Demo Html</title> </head> <body>  <H1>Testing One Two Three</H1> </body></html>";

//load from assets
//w.loadDataWithBaseURL("file:///android_asset/", Strings.converterParaElementosHTMLEspeciais(html), "text/html", "iso-8859-1", null);
//w.loadUrl("file:///android_asset/darf.html");
//w.loadUrl("https://www.google.com.br");
w.loadData(html, "text/html", "iso-8859-1");

【问题讨论】:

    标签: java android webview image capture


    【解决方案1】:

    这个错误是因为你的WebView的宽度和高度是0,所以你必须先Layout你的WebView,然后试试这个代码:

    myWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    view.post(new Runnable() {
    
                        @Override
                        public void run() {
                            // take the snapshot here
    
    
                        }
                    });
    
                }
            }
    
        });
    

    【讨论】:

    • 我所做的唯一更改是在 onProgressChanged 方法的 webview 参数中进行最终更改。 public void onProgressChanged(final WebView view, int newProgress) {
    猜你喜欢
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2022-06-25
    • 1970-01-01
    • 2017-07-31
    相关资源
    最近更新 更多