这个功能在网上看了好多人的代码,但是总是在image.writeToFile('D:/tmp/images/black.png','png')这一句出错.查了google的API也感觉没错呀。

后来自己动脑,改成了image.writeToFile('D:/tmp/images/black.png'),竟然就成功了。后面的图片后缀竟然是可以不用加的。而且

然后loadimage后面的后缀同样可选。我是这样写的 result = MonkeyRunner.loadImageFromFile('D:/tmp/images/black.png','png')

code:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
import sys,time device = MonkeyRunner.waitForConnection()
device.wake() device.startActivity(action = "android.media.action.IMAGE_CAPTURE")
MonkeyRunner.sleep(3.0)
image = device.takeSnapshot()
MonkeyRunner.sleep(1.0) if (image != None):
image.writeToFile('D:/tmp/images/black.png') cout = 1
while(cout < 1000):
device.startActivity(action = "android.media.action.IMAGE_CAPTURE")
MonkeyRunner.sleep(3.0)
image = device.takeSnapshot()
MonkeyRunner.sleep(1.0)
result = MonkeyRunner.loadImageFromFile('D:/tmp/images/black.png','png')
MonkeyRunner.sleep(3.0)
if(image.sameAs(result,0.8)):
print "success!"
else:
print "fail!"
break;
cout += 1
04-14 17:53