Pregunta

I want to snapshot the entire of the content of the WebView. However, the returned image is always blank. Can you take a look and tell me where I'm wrong.

Please don't suggest me use capturePicture function because the function is deprecated at this time.

Some codes

   public class WebViewActivity extends Activity {

        private static WebView webView;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview);

            webView = (WebView) findViewById(R.id.webView1);
    webView.loadUrl("http://developer.android.com/training/basics/firstapp/creating-project.html");

    webView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            // do your stuff here
            webView.measure(MeasureSpec.makeMeasureSpec(
                    MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            webView.layout(0, 0, webView.getMeasuredWidth(),
                    webView.getMeasuredHeight());
            webView.setDrawingCacheEnabled(true);
            webView.buildDrawingCache();
            Bitmap bm = Bitmap.createBitmap(webView.getMeasuredWidth(),
                    webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

            Canvas bigcanvas = new Canvas(bm);
            Paint paint = new Paint();
            int iHeight = bm.getHeight();
            bigcanvas.drawBitmap(bm, 0, iHeight, paint);
            if (bm != null) {
                try {
                    String path = Environment.getExternalStorageDirectory()
                            .toString();
                    OutputStream fOut = null;
                    File file = new File(path, "/aaaa.png");
                    fOut = new FileOutputStream(file);

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

The layout.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

Thank you very much!!!

¿Fue útil?

Solución

I found out the solution. Take a look of this.

I used the onDraw to replace the capturePicture functon which is deprecated in API 19. If you have any better solutions, please tell me and I appreciate that. Thanks!

Which can replace capturePicture function

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top