takeScreenshot always returns false for uiautomator

android, android-uiautomator, automation, uidevice

Solution

The takeScreenshot() method is applicable from 4.2 and above android version devices.

If the device version is appropriate then use the following piece of code.

File path = new File("/sdcard/filename.png");
int SDK_VERSION = android.os.Build.VERSION.SDK_INT; 
if (SDK_VERSION >= 17) {
    mUiAutomatorTestCase.getUiDevice().takeScreenshot(PATH);
}

We can view the file by following command.

$ adb shell ls -l /sdcard/name-of-file

Problem

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

Original source