调用draw方法后,Webview无法使用

时间:2015-11-25 14:26:07

标签: android android-webview

我想提取webview截图(即使有滚动)并保存它。 我的问题是在调用webview.draw()方法之后,webview现在是一个无法用作webview的图像。 如果有办法防止这种行为?

重要提示:我无法重新加载webview,因为用户输入了字段。

这是我的代码:

protected void onClicked() {
    mWebView.measure(View.MeasureSpec.makeMeasureSpec(
                    View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    mWebView.layout(0, 0, mWebView.getMeasuredWidth(),
            mWebView.getMeasuredHeight());

    mWebView.setDrawingCacheEnabled(true);
    mWebView.buildDrawingCache();

    generateWebViewCapture();
}

/**
 * Create capture of all webview.
 */
@Background
void generateWebViewCapture() {

    Bitmap bm = Bitmap.createBitmap(mWebView.getMeasuredWidth(),
            mWebView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bm);
    Paint paint = new Paint();
    int iHeight = bm.getHeight();
    canvas.drawBitmap(bm, 0, iHeight, paint);
    mWebView.draw(canvas);

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mWebView.setDrawingCacheEnabled(false);
            mWebView.invalidate();
        }
    });

    try {
        String path = getReportPath();
        File file = new File(path, "/report-"+System.currentTimeMillis()+".png");
        OutputStream fOut = new FileOutputStream(file);

        bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

编辑:

要解决此问题,请删除该行:

mWebView.layout(0, 0, mWebView.getMeasuredWidth(),
        mWebView.getMeasuredHeight());

1 个答案:

答案 0 :(得分:1)

获得mWebView.setDrawingCacheEnabled(false)后只需bitmap

mWebView.setDrawingCacheEnabled(true);
    // remove mWebView.buildDrawingCache();
Bitmap bm= mWebView.getDrawingCache();

 //remove   generateWebViewCapture();
   try {
    String path = getReportPath();
    File file = new File(path, "/report-"+System.currentTimeMillis()+".png");
    OutputStream fOut = new FileOutputStream(file);

    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
} catch (Exception e) {
    e.printStackTrace();
}
mWebView.setDrawingCacheEnabled(false);

此代码在我的手机上运行:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testm);
        final WebView mWebView  = (WebView) findViewById(R.id.webview);
        mWebView.loadUrl("http://www.csdn.net/");
        findViewById(R.id.iv_0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWebView.setDrawingCacheEnabled(true);
                // remove mWebView.buildDrawingCache();
                Bitmap bm= mWebView.getDrawingCache();

                //remove   generateWebViewCapture();
                try {
                    String path = getFilesDir().getPath();
                    File file = new File(path, "/report-"+System.currentTimeMillis()+".png");
                    OutputStream fOut = new FileOutputStream(file);

                    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mWebView.setDrawingCacheEnabled(false);
            }
        });
    }

}