java 截取 Android 屏幕截图并保存到 SD 卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5929403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Take Screenshot of Android screen and save to SD card
提问by Kay
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.
这里有一些关于捕获 android 应用程序屏幕截图的问题。但是,我还没有找到关于如何使用 android SDK 或任何其他方法以编程方式截取屏幕截图的可靠解决方案。
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.
所以我想我会再次问这个问题,希望我能找到一个好的解决方案,希望能够捕捉到我可以保存到 SD 卡或类似地方的全长图像。
I appreicate any help
我很感激任何帮助
回答by CommonsWare
This is not possible directly on the device/emulator, unless it is rooted.
这不能直接在设备/模拟器上进行,除非它是 root 的。
to honest all I need it for is the emulator as this is for a testing application on a PC
老实说,我需要的只是模拟器,因为这是用于 PC 上的测试应用程序
This sounds like a job for monkeyrunner.
这听起来像是monkeyrunner的工作。
回答by Vinayak Kolagi
monkeyrunner tool can do the job for you with bit of adb command, [python script]
monkeyrunner 工具可以通过一些 adb 命令为您完成这项工作,[python 脚本]
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>
注意:调用这个jython脚本文件
monkeyrunner.bat <文件名>
回答by Kaj
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/
编辑:我以前没见过这个。你试过了吗?: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 :)
Edit2:检查了那个库,它也是一个糟糕的解决方案。要求您从 PC 启动该服务。所以我最初的答案仍然成立:)
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)
Edit3:您应该能够通过执行类似于此的操作将视图另存为图像。您可能需要稍微调整一下,以便获得视图的宽度/高度。(我在夸大布局,并在布局代码时指定宽度/高度)
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();
回答by Femi
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).
您可以查看http://codaset.com/jens-riboe/droidatscreen/wiki(在http://blog.ribomation.com/2010/01/droidscreen/ 上写了一篇文章):这是一个 Java 库,使用adb
捕捉屏幕截图。我已经能够(用很多肘部油脂)修改源,让我自动捕获一系列定时屏幕截图(我用于演示视频)。
You can see the class structure at http://pastebin.com/hX5rQsSR
你可以在http://pastebin.com/hX5rQsSR看到类结构
EDIT: You'd invoke it (after bundling all the requirements) like this:
编辑:您可以像这样调用它(在捆绑所有要求之后):
java -cp DroidScreen.jar --adb "" --device "" --prefix "" --interval
java -cp DroidScreen.jar --adb "" --device "" --prefix "" --interval