2011-06-24 28 views
4

我想要捕獲一個webview的屏幕截圖給用戶在android中的圖像,我總是以黑色圖像結束。這是正確的大小和一切只是不將偏離屏幕的WebView繪製爲位圖

這裏是我使用的代碼:

String theURL = "file:///android_asset/www/pages/page2.html"; 
     WebView webview = new WebView(ctx); 
     webview.loadUrl(theURL); 
     Bitmap bm = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888); 
     Canvas c = new Canvas(bm); 
     webview.draw(c); 
     OutputStream stream = null; 
     try { 
      stream = new FileOutputStream(Environment.getExternalStorageDirectory() +"/page.jpg"); 
      bm.compress(CompressFormat.JPEG, 80, stream); 
      if (stream != null) stream.close();    
      return new PluginResult(PluginResult.Status.OK); 
     } catch (IOException e) { 
      return new PluginResult(PluginResult.Status.ERROR, e.toString()); 
     } finally { 
      bm.recycle(); 
     }  

感謝,如果有人可以提供幫助。

回答

0

webview.loadUrl(theURL);開始加載頁面,這可能需要一段時間。在繪製你的webview之前等待,直到頁面加載完畢。您可以使用setWebViewClient(WebViewClient client)設置一個對象,以在頁面加載完成時收到通知。