用于webview的Android-Screenshot API,包括同一Web视图的屏幕外部分

时间:2017-02-08 14:22:31

标签: android android-webview

是否有任何API可用于获取包含完整WebView的WebView的屏幕截图(包括不属于当前视图但可以通过滚动WebView访问的部分)?

我在之前的问题中发现,本网站中提到的问题是使用了弃用的方法,而且这些方法没有正确呈现屏幕截图。

1 个答案:

答案 0 :(得分:0)

对于那些正在检查答案的人,从其他问题我找到了答案。

     wvWb = (WebView) findViewById(R.id.wvMy);
    wvWb.getSettings().setJavaScriptEnabled(true);
    wvWb.loadUrl("http://www.entri.me");
    wvWb.setWebViewClient(new WebViewClient() {
     @Override
        public void onPageFinished(WebView view, String url) {
          String root = Environment.getExternalStorageDirectory().toString();
            File myDir = new File(root + "/11111");
            myDir.mkdirs();
            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            String fname = "Image-" + n + ".jpg";
            File file = new File(myDir, fname);
            wvWb.setDrawingCacheEnabled(true);
         wvWb.setDrawingCacheEnabled(false);
         wvWb.measure(android.view.View.MeasureSpec.makeMeasureSpec(
                    android.view.View.MeasureSpec.UNSPECIFIED, android.view.View.MeasureSpec.UNSPECIFIED),
                    android.view.View.MeasureSpec.makeMeasureSpec(0, android.view.View.MeasureSpec.UNSPECIFIED));
            wvWb.layout(0, 0, wvWb.getMeasuredWidth(),
                    wvWb.getMeasuredHeight());
            wvWb.setDrawingCacheEnabled(true);
            wvWb.buildDrawingCache();
            Bitmap bm = Bitmap.createBitmap(wvWb.getMeasuredWidth(),
                    wvWb.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

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


            if (bm != null) {

                try {
                    OutputStream fOut = null;
                    fOut = new FileOutputStream(file);

                    bm.compress(Bitmap.CompressFormat.PNG, 50, fOut);

                    fOut.flush();
                    fOut.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }
    });