2014-01-07 69 views
1

我想快照整个WebView的内容。但是,返回的图像始终为空。你可以看看并告诉我我错在哪里。快照webview返回空白图像

请不要建议我使用capturePicture函数,因为此时该函数已被弃用。

一些代码

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

非常感谢您!

回答

0

我找到了解决方案。看看这个。

我用的onDraw,以取代在API 19.弃用如果您有什么更好的解决方案capturePicture functon,请告诉我,我对此表示赞赏。谢谢!

Which can replace capturePicture function