Take Screenshot of Android screen and save to SD c

2019-02-20 04:19发布

问题:

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

回答1:

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.



回答2:

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>



回答3:

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();


回答4:

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