1

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.

  1. the program does not terminate after getting the screenshot - so start.waitFor does not quite work as I wanted it to work

  2. currently my code reads characters, where i actually want to read bytes

  3. 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!

Lukas Makor
  • 1,947
  • 2
  • 17
  • 24

2 Answers2

5

Why complicating things. If you are invoking adb and want its output just run

adb exec-out screencap -p > myimg.png

exec-out is used instead of shell to get raw data (i.e. the image).

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • There are quite a few problems with the OP's question but the general intention of running `adb` command from a `java` program and capturing its output without having to save it to a file is absolutely fine. If you do not agree with the premise - that discussion belongs to the comments section. – Alex P. Jul 16 '17 at 21:49
  • thank you for your answer! I tried the exec-out command and got it working correctly. However performance was not as good as i expected (~1FPS). So I searched further and found a library which i can use, such that I don´t have to write everything manually – Lukas Makor Jul 20 '17 at 08:51
0

After searching some more time I came across ddmlib which already has the functionality to take screenshots and perform various other tasks via ADB built in.
The library works great and definitely made it easier for me to execute commands via ADB.

Lukas Makor
  • 1,947
  • 2
  • 17
  • 24