Home:ALL Converter>takeScreenshot always returns false for uiautomator

takeScreenshot always returns false for uiautomator

Ask Time:2013-12-03T07:25:41         Author:rumit patel

Json Formatter

I am trying to automate 'taking a screenshot' on Galaxy S4 and Kindle HDX 8.9 and I am using the following code.

if(!(getUiDevice().takeScreenshot(new File("ANYPATH"))))
         System.out.println("False: Screenshot not taken!!");
     else
         System.out.println("Gangnam Style...");

ANYPATH values I tried:

  • /data/local/tmp/ (For both devices) . Not sure where would I find this folder on the device, I tried this because I pushed my jars to this location.
  • /sdcard/pictures/ (For Kindle HDX)
  • /storage/emulated/0 (for Galaxy S4)

Irrespective of the path I try, the condition always returns false and the screenshot is not taken on any of the devices (actual devices and not an emulator). I am not sure what am I missing here?

I am just following the syntax from http://developer.android.com/tools/help/uiautomator/UiDevice.html#takeScreenshot(java.io.File)

Regards, Rumit

Author:rumit patel,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/20339939/takescreenshot-always-returns-false-for-uiautomator
Rajesh :

The takeScreenshot() method is applicable from 4.2 and above android version devices.\n\nIf the device version is appropriate then use the following piece of code.\n\nFile path = new File(\"/sdcard/filename.png\");\nint SDK_VERSION = android.os.Build.VERSION.SDK_INT; \nif (SDK_VERSION >= 17) {\n mUiAutomatorTestCase.getUiDevice().takeScreenshot(PATH);\n}\n\n\nWe can view the file by following command.\n\n$ adb shell ls -l /sdcard/name-of-file",
2013-12-20T07:07:58
user3076495 :

I had the same issue and switched to using the adb screencap function instead. I guess this is not an answer, but a workaround:\n\nProcess process = Runtime.getRuntime().exec(\"screencap -p \" + <filePath>);\nprocess.waitFor();\n",
2013-12-07T01:06:30
yy