当我尝试获取整个页面的屏幕截图时 returns 出现白屏?

When I try to get whole page screenshot it returns a white screen?

我试图在 webview 中以编程方式获取整个屏幕截图。当我测试我的代码时,returns 出现白屏。

我尝试了不同的代码,但无法完成。有时 returns 是白屏,有时 returns 是黑屏。我还添加了我的清单 Internet 权限并编写了外部存储权限。我的 xml 文件只存在于 webview 中。

我也试过 "sleep" 五秒钟,但没用。

public static Bitmap loadBitmapFromView(WebView v, int width, int height) 
{
    Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);  
    FileOutputStream fos = null;
    try 
    {
        fos = new FileOutputStream("/sdcard/image.jpg" );
    } 
    catch (FileNotFoundException e1) 
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    b.compress(Bitmap.CompressFormat.JPEG, 90, fos);

    try 
    {
        fos.close();
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return b;
}

我在onpageFinished()中调用了这个方法。通过 loadBitmapFromView(webView, webView.getWidth(), webView.getHeight()); 我确保加载整个页面。在我看来,当 webview 在调用之前未加载时,它会尝试获取屏幕截图。但是我在 onpageFinished() 方法中调用它。它在 webview 完全加载时运行。

Other parts of my code
public class Wv extends Activity 
{  

    private static WebView webView;
    CustomWebViewClient webViewClient;
    ProgressDialog mProgressDialog;
      public void onCreate(Bundle savedInstanceState) 
      {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView1);

        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Loading...");

        webView.loadUrl("file:///sdcard/ZipDemo/HTML_Friday.html");
        webViewClient = new CustomWebViewClient();         
        webView.setWebViewClient(webViewClient);     
    }


      private class CustomWebViewClient extends WebViewClient 
        {

            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                   super.onPageStarted(view, url, favicon);

                if(!mProgressDialog.isShowing())
                {
                    mProgressDialog.show();
                }
            }  
            @Override 
            public void onPageFinished(WebView view, String url) {


                   super.onPageFinished(view, url); 
                if(mProgressDialog.isShowing()){
                    mProgressDialog.dismiss();
                }
loadBitmapFromView(webView,webView.getWidth(),webView.getHeight());

         }
       }

您是否启用了 View 的 DrawingCache?

public static Bitmap getBitmapFromView(View view) {
    try {
      view.setDrawingCacheEnabled(true);
      view.buildDrawingCache(true);
      Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
      view.setDrawingCacheEnabled(false); // clear drawing cache
      return bitmap;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

您应该在视图的 post-方法中使用它:(http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable))

view.post(new Runnable() {
    @Override
    public void run() {

    }
  });

通过删除 CustomWebViewClient class 并创建新的 button.onPageFinished() 方法解决了问题,但效果不佳。当我创建按钮时,问题就解决了。 这是代码。

public class Wv extends Activity 
{  
    private WebView webView;
    private Button screenShot;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView1);
        screenShot=(Button) findViewById(R.id.button_screenshot);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);

        webView.loadUrl("file:///android_asset/HTML_Friday.html");

      screenShot.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub          

            Picture picture = webView.capturePicture();
            Bitmap b = Bitmap.createBitmap(picture.getWidth(),picture.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);

            File screen = new File(Environment.getExternalStorageDirectory()
                    .toString(), "tt12.jpg");
            if (screen.exists())
                screen.delete();

            picture.draw(c);

            FileOutputStream fos = null;
            try {

                fos = new FileOutputStream(screen);
                if (fos != null) {
                    b.compress(Bitmap.CompressFormat.JPEG, 90, fos);

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

            }
        }
    });
   }    
 }