【问题标题】:How to print whole WebView(Scrollable)?如何打印整个 WebView(可滚动)?
【发布时间】:2017-09-06 06:59:01
【问题描述】:

我想在按钮单击时打印整个WebView

我用的是西铁城打印机。 我尝试了以下方式。

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?

【问题讨论】:

标签: android printing webview


【解决方案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使用webView.getDrawingCache();

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());
        }
    }
}

【讨论】:

  • 现在我的方法也可以了,不需要将文件存储在SD卡中。我只是在我的代码中添加if(Build.VERSION.SDK_INT&gt;=Build.VERSION_CODES.LOLLIPOP) { WebView.enableSlowWholeDocumentDraw(); },我的代码工作正常。感谢您的帮助。
  • 给你测试一下就知道了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 2020-12-13
  • 2011-09-09
相关资源
最近更新 更多