调用onPageFinished()

时间:2019-04-02 08:57:55

标签: java android android-webview webpage-screenshot

我想在页面加载完成后拍摄网络视图的屏幕截图。因此,我尝试在getSnapShot()中添加onPageFinished(),但是在调用getSnapShot()时,该页面未加载。因此快照的图片是空白的。

并且我加载了本地html文件url,所以我相信没有重定向。

我还尝试使MyWebView extends WebView,覆盖onDraw(),添加回调侦听器以在getSnapShot()进程中调用onDraw()。但仍然无法正常工作。

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        // will get a blank picture
        getSnapshot();
    }
});

1 个答案:

答案 0 :(得分:1)

尝试此代码;

 webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
    Picture picture = view.capturePicture();
        Bitmap  b = Bitmap.createBitmap( picture.getWidth(),
        picture.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas( b );

         picture.draw( c );
         FileOutputStream fos = null;
         try {

       fos = new FileOutputStream( "your_directory_screenshot.jpg" );
          if ( fos != null )
         {
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);

      fos.close();
               }
         }
        catch( Exception e )
      {}
    }
});