Saturday, February 4, 2012

In App Screenshots

One of the new highly touted features in Ice Cream Sandwich is screenshot support. This allows a user to take a snapshot of their screen and send it to anyone. Obviously this is a valuable debugging tool as users can send how an app looked when it did something weird.

It is possible to partially support screenshots within your own app. This is essentially done by getting the content root view of your Activity and rendering it against a Canvas.

The code below does just that. (Source excerpt from the android-beryl library.)
public class Screenshot {
  private final View view;
  /** Create snapshots based on the view and its children. */
  public Screenshot(View root) {
    this.view = root;
  }
  /** Create snapshot handler that captures the root of the whole activity. */
  public Screenshot(Activity activity) {
    final View contentView = activity.findViewById(android.R.id.content);
    this.view = contentView.getRootView();
  }
  /** Take a snapshot of the view. */
  public Bitmap snap() {
    Bitmap bitmap = Bitmap.createBitmap(this.view.getWidth(), this.view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
  }
}

4 comments:

  1. Interesting, but shouldn't a file for the screenshot picture be created and saved?

    ReplyDelete
    Replies
    1. You can save the bitmap to a file by using Bitmap.compress() as described here: http://stackoverflow.com/a/673014/379245

      Delete
  2. what code to share image on social

    ReplyDelete
  3. LuckyLand Casino (Washington, D.C) - Mapyro
    This site 김포 출장마사지 is no 김해 출장마사지 longer available. See 안동 출장마사지 map. LuckyLand Casino is open. Please contact us for 태백 출장마사지 questions or suggestions. Rating: 5 · 광양 출장샵 ‎1 review

    ReplyDelete