Android 屏幕截图并保存到 SD 卡

发布于 2024-11-05 23:17:25 字数 174 浏览 2 评论 0 原文

这里有一些关于捕获 Android 应用程序屏幕截图的问题。但是,我还没有找到关于如何使用 android SDK 或任何其他方法以编程方式截取屏幕截图的可靠解决方案。

所以我想我会再次问这个问题,希望我能找到一个好的解决方案,希望能够捕获全长图像,并将其保存到 SD 卡或类似的地方。

我感谢任何帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光与爱终年不遇 2024-11-12 23:17:25

这不可能直接在设备/模拟器上实现,除非它已获得 root 权限。

说实话,我需要的只是模拟器,因为这是用于 PC 上的测试应用程序

这听起来像是 monkeyrunner

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.

笨死的猪 2024-11-12 23:17:25

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')

注意:调用此 jython 脚本文件
Monkeyrunner.bat <文件名>;

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>

那支青花 2024-11-12 23:17:25

您很可能会对这个答案不满意,但我见过的唯一答案涉及使用本机代码或执行本机命令。

编辑:
我以前没见过这个。你尝试过吗?:
http://code.google.com/p/android-screenshot-library/

Edit2:检查了该库,这也是一个糟糕的解决方案。要求您从 PC 启动该服务。所以我最初的答案仍然成立:)

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

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();
罪#恶を代价 2024-11-12 23:17:25

您可以查看 http://codaset.com/jens-riboe/droidatscreen/wiki (写在 http://blog.ribomation.com/2010/01/droidscreen/):这是一个使用 adb 捕获屏幕截图的 Java 库。我已经能够(费了很多功夫)修改源代码,让我自动捕获一系列定时的屏幕截图(我将其用于演示视频)。

您可以在 http://pastebin.com/hX5rQsSR 中查看类结构

编辑:您可以调用它(之后捆绑所有要求)如下所示:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文