Search code examples
androidandroid-webview

Webview unusable after draw method called


I want to extract webview screenshot (even if there is a scroll) and to save it. My problem is after webview.draw() method called, webview is now an image an is unusable as a webview. If there a way to prevent that behavior ?

Important : I can't just reload webview because user enters fields.

Here's my code :

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

EDIT :

To fix the problem, remove that line :

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

Solution

  • Just mWebView.setDrawingCacheEnabled(false) after you get the 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);
    

    This code is worked on my mobile:

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