I know that one can take a screenshot from the Android device via ADB with
$ adb shell screencap -p /mnt/sdcard/sc.png
$ adb pull /mnt/sdcard/sc.png
However this writes a file on your phone and on your PC, which I want to avoid.
So I found the following SO question and the answer suggested that the image gets printed to the Std output when you do not specify a file. I tested this from console and it really printed binary data to the console.
Android: It there a way to read screenshot from memory without saving to internal/external storage?
Now I want to utilize this technique and start a process from java, execute the
adb shell screencap
command, read the output and create a BufferedImage from the output.
I tried something like this
ProcessBuilder pb = new ProcessBuilder("cmd");
Process start = pb.start();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write("adb shell screencap");
bw.newLine();
bw.flush();
// wait some time for the process to print the image to the console
start.waitFor(10, TimeUnit.SECONDS);
StringBuilder sb = new StringBuilder(9000000);
Scanner s = new Scanner(start.getInputStream());
while (s.hasNext()) {
sb.append(s.next());
}
String result = sb.toString();
Unluckily there are quite a few issues with my Code.
the program does not terminate after getting the screenshot - so start.waitFor does not quite work as I wanted it to work
currently my code reads characters, where i actually want to read bytes
- reading with scanner seems kind of slow when reading millions of characters/bytes
Maybe someone can point me in a direction such that I can get it to work. Thanks!