Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically take a screenshot of a webview, capturing the full page?

I think the title pretty much covers it, but I have a webview in my activity. I've loaded a url into the webview and I'd like to take a screenshot of the full page (whatever is in the viewport and the stuff "below the fold" as well).

I've got code that works to snapshot the viewport, and I know this can be done in iOS by enlarging the webview before snapshotting it. I've tried to use the same technique here:

    WebView browserView = (WebView) findViewById(R.id.browserView);      //Resize the webview to the height of the webpage     int pageHeight = browserView.getContentHeight();     LayoutParams browserParams = browserView.getLayoutParams();     browserView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));      //Capture the webview as a bitmap     browserView.setDrawingCacheEnabled(true);     Bitmap bitmap = Bitmap.createBitmap(browserView.getDrawingCache());     browserView.setDrawingCacheEnabled(false);      //Create the filename to use     String randomFilenamepart = String.valueOf(new SecureRandom().nextInt(1000000));     String filename = Environment.getExternalStorageDirectory().toString() + "/Screenshot_" + randomFilenamepart + ".jpg";     File imageFile = new File(filename);     //Stream the file out to external storage as a JPEG     OutputStream fout = null;     try {         fout = new FileOutputStream(imageFile);         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);         fout.flush();         fout.close();     } catch (FileNotFoundException e) {         e.printStackTrace();     } catch (IOException e) {         e.printStackTrace();     } finally {         browserView.setLayoutParams(browserParams);     } 

But I'm still only capturing just the viewport. Disregarding things like running out of memory because the page is too large, does anyone know what I'm doing wrong or how I can include the portion outside the viewport?

like image 240
Mark Rausch Avatar asked Mar 16 '12 23:03

Mark Rausch


People also ask

How do I screenshot an entire web page on Android?

Hold the Volume Down and Power Button. An Expanded Screenshot button will appear in the lower right corner of the screen. Select that. The scrolling screenshot will be captured automatically.

How can I capture an entire Web page on my phone?

On Android, start by tapping the 'V' icon at the top of the screen and select “Capture page”. Then choose whether you want to take a full page screenshot, or just capture the “Visible Area” (which is, a screenshot of what you can currently see on your screen). The image will be saved to your device.


2 Answers

Try this one

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;     } }  

Add INTERNET PERMISSION and WRITE_EXTERNAL_STORAGE in AndroidManifest.xml file.

Need to ask permission at run time for file write if app is running on or above Marshmallow.

like image 185
avinash Avatar answered Sep 19 '22 14:09

avinash


webview.capturePicture() is deprecated, use below way can do:

Bitmap bitmap = Bitmap.createBitmap(webview.getWidth(), webview.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); webview.draw(canvas); return bitmap; 
like image 34
Amos Avatar answered Sep 21 '22 14:09

Amos