快照webview返回空白图像

时间:2014-01-07 02:17:58

标签: android webview snapshot-view

我想要快照WebView的整个内容。但是,返回的图像始终为空白。你能看看我告诉我哪里错了。

请不要建议我使用capturePicture函数,因为此时不推荐使用该函数。

一些代码

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

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"
/>

非常感谢!!!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。看看这个。

我使用onDraw替换了API 19中不推荐使用的capturePicture功能。如果您有更好的解决方案,请告诉我,我很感激。谢谢!

Which can replace capturePicture function