捕获webview时,不显示视图的内容为白色

时间:2016-07-11 11:13:34

标签: android webview capture

我对Webview有疑问。

这是我的来源和结果。

private void saveDocumentImage() {

    WebView wv = arrWebView.get(0);

    wv.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int y=wv.getHeight();

    Bitmap captureView = Bitmap.createBitmap(wv.getMeasuredWidth(), wv.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    wv.layout(0, 0, wv.getMeasuredWidth(), wv.getMeasuredHeight());
    Canvas screenShotCanvas = new Canvas(captureView);
    wv.draw(screenShotCanvas);


    if (captureView != null) {
        try {
            String path = Environment.getExternalStorageDirectory().toString();
            OutputStream fOut = null;
            File file = new File(path, "temp");

            if (!file.exists())
                file.mkdirs();

            fOut = new FileOutputStream(path + "/temp/test_0.jpg");
            captureView.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            wv.setDrawingCacheEnabled(false);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    wv.destroyDrawingCache();
    captureView.recycle();
    captureView=null;
}

这里和我一样。  :https://stackoverflow.com/questions/37023619/capturing-bitmap-android-chromium-webview-returns-content-as-white

1 个答案:

答案 0 :(得分:2)

U无法拍摄网页视图和表面视图的屏幕截图,因为它们的内容已加载到其持有者中,而持有者不是视图。如果你找到一种方法然后分享它。有一次我对它进行了一些研究并发现this但我还没有测试过它。尝试它,如果它适合你然后接受我的答案:D。祝你好运