質問

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

役に立ちましたか?

解決

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

他のヒント

I had the same issue and switched to using the adb screencap function instead. I guess this is not an answer, but a workaround:

Process process = Runtime.getRuntime().exec("screencap -p " + <filePath>);
process.waitFor();

I had this problem too. And with help of debug tools, I found it is a permission problem. I solved it by adding storage write permission in my manifest. If you are using an 6.0+ device, you also have to manually authorize permission to your test app in runtime.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top