1

There are a few questions here on SO about capturing screenshots of an android application. However, I haven't found a solid solution on how to take a screenshot programatically using the android SDK or any other method.

So I thought I would ask this question again in the hopes that I can find a good solution, hopefully one that will allow capturing full length images that I can save to the SD card or somewhere similar.

I appreicate any help

Kay
  • 845
  • 7
  • 21
  • 33
  • 2
    We had a similar question but I found an answer. Here's a solution: http://stackoverflow.com/questions/5939987/android-take-screenshot-via-code – Peanut May 13 '11 at 14:02

4 Answers4

3

This is not possible directly on the device/emulator, unless it is rooted.

to honest all I need it for is the emulator as this is for a testing application on a PC

This sounds like a job for monkeyrunner.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Out of curiosity, do you know if there is a reason why Android can't do this? Is it a policy decision by the Android developers or is it a limitation somewhere in Android itself? – Squonk May 09 '11 at 17:11
  • @MisterSquonk: It is a policy decision to not have an API for it, AFAIK. Why they don't have their own OS-supplied app for screenshots, I can't say. – CommonsWare May 09 '11 at 18:24
  • Thanks. Not that I personally have a use for it - I just find it curious that it isn't a standard feature. – Squonk May 09 '11 at 19:53
  • As the MonkeyRunner doc explains, it's easy to assume that MonkeyRunner is just a runner for the Monkey tool, in which case one (aka, I) might skip the MonkeyRunner doc altogether. I suggest adding "NOT related to Monkey" to the entry in the tools TOC. – cdhabecker Aug 08 '11 at 20:09
1

monkeyrunner tool can do the job for you with bit of adb command, [python script]

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
//waits for connection
device = MonkeyRunner.waitForConnection()
//take the current snapshot
device.takeSnapshot()
//stores the current snapshot in current dir in pc
device.writeToFile('current.png')\
//copy it to the sd card of device
os.subprocess.call('adb push current.png /sdcard/android/com.test.myapp/current.png')

Note: call this jython script file
monkeyrunner.bat <file name>

Vinayak Kolagi
  • 1,831
  • 1
  • 13
  • 26
0

You will most likely not be happy with this answer, but the only ones that I have seen involve using native code, or executing native commands.

Edit: I hadn't seen this one before. Have you tried it?: http://code.google.com/p/android-screenshot-library/

Edit2: Checked that library, and it also is a bad solution. Requires that you start the service from a pc. So my initial answer still holds :)

Edit3: You should be able to save a view as an image by doing something similar to this. You might need to tweek it a bit so that you get the width/height of the view. (I'm inflating layouts, and specify the width/height when I layout the code)

View content = getView();
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
File file = new File(pathAndFilename);              
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
Kaj
  • 10,862
  • 2
  • 33
  • 27
  • to honest all I need it for is the emulator as this is for a testing application on a PC. I have had a look at that library before and I couldn't find a way to turn it from a background service to a class. If I use it as a service it requires that I need to start it every time after a reboot, which is not ideal for me. – Kay May 08 '11 at 18:40
  • Are you saying that you have a view that you have created, and you want to take a screenshot of that view? That is not that hard. – Kaj May 08 '11 at 18:53
  • that is exactly what I am saying. It would be great if I could get the full length of the view too (its scrollable) but that might not be possible. – Kay May 08 '11 at 18:55
  • See my edited answer. I posted some code that you should be able to use. – Kaj May 08 '11 at 20:02
  • 1
    if just from the emulator, why not just use the screenshot button in eclipse (Devices Perspective) – jkhouw1 May 09 '11 at 17:25
0

You can look at http://codaset.com/jens-riboe/droidatscreen/wiki (with a write up at http://blog.ribomation.com/2010/01/droidscreen/): this is a Java library that uses adb to capture a screen shots. I've been able to (with a lot of elbow grease) modify the source to let me automatically capture a timed series of screen shots (which I use for demo videos).

You can see the class structure at http://pastebin.com/hX5rQsSR

EDIT: You'd invoke it (after bundling all the requirements) like this:

java -cp DroidScreen.jar --adb "" --device "" --prefix "" --interval

Femi
  • 64,273
  • 8
  • 118
  • 148