2017-01-02 43 views
0

我試圖採取的Android的WebView的全貌,但只有設備的可視區域捕捉等領域得到變灰out.Here是代碼:無法捕捉到的WebView的全快照的Android

public Bitmap getWebViewFullImage(final WebView webView) { 
    try { 
     Picture picture = webView.capturePicture(); 
     int width=picture.getWidth(); 
     int height=picture.getHeight(); 
     Bitmap bm = Bitmap.createBitmap(width, height 
       , Bitmap.Config.ARGB_8888); 

     Canvas bigcanvas = new Canvas(bm); 
     picture.draw(bigcanvas); 
     return bm; 
    } 
    catch (Exception e) { 
     return null; 
    } 
} 

回答

1
import java.io.FileOutputStream; 
    import android.app.Activity; 
    import android.graphics.Bitmap; 
    import android.graphics.Canvas; 
    import android.graphics.Picture; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.webkit.WebView; 
    import android.webkit.WebViewClient; 

     public class MainActivity extends Activity { 

WebView w ; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    w = new WebView(this); 
    w.setWebViewClient(new WebViewClient() 
    { 
      public void onPageFinished(WebView view, String 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("mnt/sdcard/yahoo.jpg"); 
          if (fos != null) 
          { 
           b.compress(Bitmap.CompressFormat.JPEG, 100, fos); 

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

        } 
      } 
     }); 

    setContentView(w); 
    w.loadUrl("http://search.yahoo.com/search?p=android"); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 
} 

這段代碼就足以說明你需要的一切

+0

我在onPageFinished中調用了getWebViewFullImage函數。 –