当我尝试获取整页截图时,会返回白屏?

时间:2015-07-20 08:38:44

标签: android webview screenshot

我试图在webview中以编程方式获取整个屏幕截图。当我测试我的代码时,它会返回一个白色屏幕。

我尝试过不同的代码,但我无法完成。有时会返回白色屏幕,有时会返回黑屏。我还添加了我的清单Internet权限并写了一个外部存储权限。我的xml文件仅存在于webview中。

我也尝试过睡眠"五秒钟,但它没有工作。

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

         }
       }

2 个答案:

答案 0 :(得分:0)

您是否启用了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;
    }
  }

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

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

    }
  });

答案 1 :(得分:0)

通过删除CustomWebViewClient类并创建新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();

            }
        }
    });
   }    
 }