2012-11-21 4 views
2

webiview에서 이미지를 캡처하려고하는데, 웹에서 loadUrl을 시도 할 때 작동하지만, 자산 또는 HTML에서 로컬 html 파일을로드하려고하면 작동합니다.webview에서 가져 오기 그림 로컬 로컬 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) 

내 코드는 다음과 같습니다 : 당신의 WebView 폭과 높이가 0입니다

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

답변

2

이 오류 때문에, 당신이 당신의 WebView 첫째, 다음 시도이 코드를 Layout해야한다 있도록 : 다음과 같은 오류 crashs

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 


        } 
       }); 

      } 
     } 

    }); 
+0

고마워요! 그것은 효과가있다! – JosafaSSJ

+0

다음 대답을 수락 –

+0

내가 만든 유일한 변경 사항은 onProgressChanged 메서드의 webview 매개 변수에 마지막으로 넣어졌습니다. public void onProgressChanged (최종 WebView 뷰, int newProgress) { – JosafaSSJ

관련 문제