Running the command: adb shell screencap /sdcard/image1.png from the command line works, and the screenshot is taken and saved in the gallery as "image1.png"
But this doesn't work grammatically when doing:
String[] commandAndArgs = new String[]{ "screencap", "/sdcard/qweqwe.
Process p = Runtime.getRuntime().exec(commandAndArgs);
No errors or exception are thrown
Full code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
String[] commandAndArgs = new String[]{ "screencap", "/sdcard/qweqwe.png" };
Process p = Runtime.getRuntime().exec(commandAndArgs);
String error = "error: ";
Scanner scanner = new Scanner(p.getInputStream());
while (scanner.hasNext()) {
error += scanner.nextLine();
}
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}catch (Exception e){
Log.e("err", Log.getStackTraceString(e));
}
}
You'll have to check if exit code of the process is equal to zero, which indicates success.
The other thing is that taking screenshots requires
android.permission.READ_FRAME_BUFFER, which is a signature permission only granted to system apps (including ADB shell). See, if you open a shell from your app it still runs with your app's permissions. And if your app doesn't have the permission it won't work.