2014-01-06 111 views
18

我有個問題。此時,WebView的capturePicture已棄用哪能代替capturePicture函數

我想問是否有方法來替換函數。我的意思是它可以將網頁流量的捕獲整個(不僅顯示視圖)

感謝

+1

對於應用定位API級21(棒棒糖)和a波夫,請也看到這個問題:http://stackoverflow.com/questions/30078157/webview-draw-not-properly-working-on-latest-android-system-webview-update –

回答

20

我終於找到了解決辦法。

有些代碼

public class WebViewActivity extends Activity { 

    private static WebView webView; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.webview); 

     webView = (WebView) findViewById(R.id.webView1); 
     webView.loadUrl("http://developer.android.com/reference/packages.html"); 
//  webView.loadUrl("http://developer.android.com/training/basics/firstapp/creating-project.html"); 

     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("1111111111111111111111=" 
         + bigcanvas.getWidth()); 
       System.out.println("22222222222222222222222=" 
         + 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(); 
        } 
       } 
      } 
     }); 
    } 
} 

的layout.xml的

<?xml version="1.0" encoding="utf-8"?> 
<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/webView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
/> 
+2

得到錯誤java.lang.IllegalArgumentException:寬度和高度必須> 0 請幫我 –

+0

@YogeshLakhotia:確保在你獲得高度或寬度之前完全加載webview。我使用AsyncTask。我不知道爲什麼,但onPageFinished可以在您的webview完全加載之前運行。我遇到了同樣的問題。我沒有看到你的代碼,所以我只是建議你一些情況。 – lolyoshi

+0

我的問題解決了......我已經回答了代碼。 –

15

draw()使用web視圖

的方法

實施例:

ImageView imageview; 
WebView webview; 
class Background extends AsyncTask<Void, Void, Bitmap> 
{ 
    @Override 
    protected Bitmap doInBackground(Void... params) 
    { 
     try 
     { 
      Thread.sleep(2000); 
      Bitmap bitmap = Bitmap.createBitmap(webview.getWidth(), webview.getHeight(), Config.ARGB_8888); 
      Canvas canvas = new Canvas(bitmap); 
      webview.draw(canvas); 
      return bitmap; 
     } 
     catch (InterruptedException e){} 
     catch (Exception e){} 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Bitmap result) 
    { 
     imageview.setImageBitmap(result); 
    } 
} 


webview.setWebChromeClient(new WebChromeClient() 
{ 
    public void onProgressChanged(WebView view, int progress) 
    { 
     if(progress==100) 
      new Background().execute(); 
    } 
}); 
+1

以7-8種方式工作,但只有這種方法適用於我的情況:) – userAndroid