Tuesday, December 2, 2008

Using the AndroidDebugBridge API to get screenshots

I needed a way to get periodic screenshots of the Android device connected to my computer through the USB port. DDMS has the ability to take screenshots on-demand, but not automatically. I dug through the DDMS source code to figure out how screenshots were taken, then a small utility in Java to do what I want. The code uses the AndroidDebugBridge API into the adb service. The jar that contains this API is ddmlib.jar, in the tools\lib directory of the Android SDK. The code is available here. It has a few limitations:
  • If there are more than one Android device connected (including emulators), it only gets screenshots from the first one we find. Ideally there would be a way to list devices and select which one we want to use.
  • Not at all configurable.
Here's what the program does: 1. Initialize the adb interface and get a handle to the bridge:
AndroidDebugBridge.init(false);
AndroidDebugBridge adb = AndroidDebugBridge.createBridge();
2. Set up a listener (an implementation of the IDeviceChangeListener interface) for device changes and add it:
Screenshot ss = new Screenshot();
AndroidDebugBridge.addDeviceChangeListener(ss);
3. Wait for a device to be connected
4. Get a list of connected devices:
Device[] devices = adb.getDevices();
5. In a loop, grab a raw image from the device, then save it as a PNG:
raw = device.getScreenshot();
PaletteData pal = new PaletteData(0xf800, 0x07e0, 0x001f);
ImageData image = new ImageData(raw.width, raw.height, raw.bpp, pal, 1, raw.data);

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image };
loader.save(filename, SWT.IMAGE_PNG);

And that's it. There's a lot more functionality in ddmlib.jar that could be useful for automating tasks like this.

5 comments:

Sanika said...

Hi,
I tried running the program but i get the following error :
Exception in thread "main" org.eclipse.swt.SWTException: Unsupported or unrecognized format
at org.eclipse.swt.SWT.error(SWT.java:3374)
at org.eclipse.swt.SWT.error(SWT.java:3297)
at org.eclipse.swt.SWT.error(SWT.java:3268)
at org.eclipse.swt.internal.image.PNGFileFormat.unloadIntoByteStream(PNGFileFormat.java:146)
at org.eclipse.swt.internal.image.FileFormat.unloadIntoStream(FileFormat.java:122)
at org.eclipse.swt.internal.image.FileFormat.save(FileFormat.java:110)
at org.eclipse.swt.graphics.ImageLoader.save(ImageLoader.java:199)
at org.eclipse.swt.graphics.ImageLoader.save(ImageLoader.java:240)
at com.codetastrophe.android.screenshot.Screenshot.getScreenshots(Screenshot.java:98)
at com.codetastrophe.android.screenshot.Screenshot.main(Screenshot.java:44)

If I change SWT.IMG_PNG to SWT.IMG_JPEG, the program works fine. Could you suggest a solution?

Unknown said...

Hi,

I am getting the following problem while trying to create bridge.

12-14 16:34:39.149: INFO/System.out(428): 04:39 E/DeviceMonitor: adb refused request: closed
12-14 16:34:39.220: INFO/System.out(428): 04:39 E/DeviceMonitor: Failed to read the adb response!

please help if anybody having any idea.

Regards,
r3

Nelson Glauber said...

Thanks a lot man! You solved my problem. Best regards from Brazil.

I post your tip in my blog too.
http://nglauber.blogspot.com/2011/05/screenshot-no-android.html

Anonymous said...

The Best Online Casinos - Casinowowowed.com
If you want to try out the most popular slots for real money, then 10bet Casinowoworld 퍼스트카지노 Casino is the place 1xbet for you! Find the best and newest casinos from

wahidawadler said...

Playing at any of those will provide you with|provides you with} a fair chance of profitable. The greatest playing websites will of course have games software from top developers, corresponding to Playtech, BetSoft and Microgaming. You can certain to|make positive to|remember to} discover slots with nice graphics and plenty of dynamic options that will run smoothly, whether it’s a desktop, iPhone 카지노사이트 or mobile system that you’re using. When you play on-line slots for actual cash, you will need to be reassured that your cash is in good arms. You can depend on our recommendations being licensed by the relevant gaming authorities, and guaranteeing a fair game using random number generators to put out these profitable combos on the reels.