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