2017-09-06 44 views
1

我想在Button Click上打印整个WebView如何打印整个WebView(可滚动)?

我使用了Citizen打印机。 我试过下面的方法。

private Bitmap CitizenWebPrint(WebView webView) { 
    Bitmap bitmapCitizen = null; 
    Picture capturePicture = webView.capturePicture(); 
    int width = capturePicture.getWidth(); 
    int height = capturePicture.getHeight(); 

    try { 
     bitmapCitizen = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 
    } catch (OutOfMemoryError e) { 
     Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_LONG).show(); 
    } 
    capturePicture.draw(new Canvas(bitmapCitizen)); 

    return bitmapCitizen; 
} 

但问题是, 这只能打印的WebView可见部分。

如何打印整个WebView?

+0

的可能的复制[可替代capturePicture功能(https://stackoverflow.com/questions/20942623/which-can-replace-capturepicture-function) –

+0

@ godslayer_69我想这个问题的答案,但没有得到成功。 –

回答

1

绘制当前窗口的DecorView对象,然后绘制Bitmap对象。

你可以这样做。

在做之前,你可以做到这一点。

WebSettings webSettings = webView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webSettings.setSupportZoom(true); 
    webView.requestFocusFromTouch(); 
    webView.setWebViewClient(new WebViewClient() { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    }); 
    webView.loadUrl("your url"); 

并检查版本。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    checkSdkVersion(); 
    setContentView(R.layout.activity_webview_capture); 
} 

// check version 
private void checkSdkVersion() { 
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) { 
     WebView.enableSlowWholeDocumentDraw(); 
    } 
} 

在清单中添加权限。

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

路1

private void getScreenshot() { 
    float scale = webView.getScale(); 
    int webViewHeight = (int) (webView.getContentHeight() * scale + 0.5); 
    Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webViewHeight, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap); 
    webView.draw(canvas); 
    // save to the File 
    try { 
     String fileName = Environment.getExternalStorageDirectory().getPath() + "/webview_capture1.jpg"; 
     FileOutputStream fos = new FileOutputStream(fileName); 
     // Save 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); 
     fos.close(); 
     Toast.makeText(WebviewFromDraw.this, "Screenshot OK", Toast.LENGTH_LONG).show(); 
     bitmap.recycle(); 
    } catch (Exception e) { 
     e.getMessage(); 
    } 
} 

路2使用

private void getScreenshot() { 

    bitmap = webView.getDrawingCache(); 
    try { 
     String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_capture2.jpg"; 
     FileOutputStream fos = new FileOutputStream(fileName); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); 
     fos.close(); 
     Toast.makeText(WebviewFromDrawCache.this, "Screenshot OK", Toast.LENGTH_LONG).show(); 

    } catch (Exception e) { 
     Log.e("TAG", e.getMessage()); 
    } 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    //recycle 
    if(bitmap!=null) { 
     bitmap.recycle(); 
    } 
} 

路3同你的,它在我的设备运行良好。

private void getScreenshot() { 
    Picture picture = webView.capturePicture(); 
    int width = picture.getWidth(); 
    int height = picture.getHeight(); 
    if (width > 0 && height > 0) { 
     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     picture.draw(canvas); 
     try { 
      String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_capture3.jpg"; 
      FileOutputStream fos = new FileOutputStream(fileName); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); 
      fos.close(); 
      Toast.makeText(WebviewFromCapture.this, "Screenshot ok", Toast.LENGTH_LONG).show(); 
      bitmap.recycle(); 
     } catch (Exception e) { 
      Log.e(TAG, e.getMessage()); 
     } 
    } 
} 
+0

现在我的方法也在工作,无需将文件存储在SD卡中。 我只是加了'if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.LOLLIPOP){WebView.enableSlowWholeDocumentDraw(); }'在我的代码中,我的代码工作正常。谢谢您的帮助。 –

+0

只是给你test.And你会很容易知道它。 – KeLiuyue