Bug
On Android emulators with multiple displays (e.g. Galaxy Z Fold), agent-device screenshot produces corrupt PNG files that cannot be parsed by image tools or APIs.
Root Cause
The screenshot function in daemon.js runs:
adb exec-out screencap -p
When multiple displays exist, adb screencap writes a warning to stdout before the PNG data:
[Warning] Multiple displays were found, but no display id was specified! Defaulting to the first display found, however this default is not guaranteed to be consistent across captures. A display id should be specified. See "dumpsys SurfaceFlinger --display-id" for valid display IDs.
This warning text (347 bytes in my case) gets prepended to the PNG file, making it invalid. file reports data instead of PNG image data.
Reproduction
- Boot a Galaxy Z Fold emulator (which has two displays — inner 1840x2092 and outer 1080x2092)
- Run
agent-device screenshot /tmp/test.png
- Run
file /tmp/test.png → reports data instead of PNG image data
- Inspect with
xxd /tmp/test.png | head -3 → starts with [Warning] Multip... instead of PNG magic bytes
Suggested Fix
Either:
-
Strip pre-PNG garbage from the buffer — find the PNG signature (\x89PNG\r\n\x1a\n) in the stdout buffer and discard everything before it. This is the most robust approach since it handles any unexpected stderr-to-stdout leakage.
-
Pass an explicit display ID — query dumpsys SurfaceFlinger --display-id and pass -d <id> to screencap to suppress the warning. This is fragile since the display IDs are hardware-specific.
Environment
- agent-device v0.7.6
- Android emulator: Galaxy Z Fold (API 35)
- macOS
Bug
On Android emulators with multiple displays (e.g. Galaxy Z Fold),
agent-device screenshotproduces corrupt PNG files that cannot be parsed by image tools or APIs.Root Cause
The screenshot function in
daemon.jsruns:When multiple displays exist,
adb screencapwrites a warning to stdout before the PNG data:This warning text (347 bytes in my case) gets prepended to the PNG file, making it invalid.
filereportsdatainstead ofPNG image data.Reproduction
agent-device screenshot /tmp/test.pngfile /tmp/test.png→ reportsdatainstead ofPNG image dataxxd /tmp/test.png | head -3→ starts with[Warning] Multip...instead of PNG magic bytesSuggested Fix
Either:
Strip pre-PNG garbage from the buffer — find the PNG signature (
\x89PNG\r\n\x1a\n) in the stdout buffer and discard everything before it. This is the most robust approach since it handles any unexpected stderr-to-stdout leakage.Pass an explicit display ID — query
dumpsys SurfaceFlinger --display-idand pass-d <id>toscreencapto suppress the warning. This is fragile since the display IDs are hardware-specific.Environment