1

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

布局.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"
/>

非常感谢!!!

4

1 回答 1

0

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

我使用 onDraw 替换了 API 19 中已弃用的 capturePicture 函数。如果您有更好的解决方案,请告诉我,我很感激。谢谢!

哪个可以代替 capturePicture 功能

于 2014-01-07T02:57:16.653 回答