Skip to content

Screenshots corrupted on multi-display Android emulators (e.g. Galaxy Fold) #184

@spalger

Description

@spalger

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

  1. Boot a Galaxy Z Fold emulator (which has two displays — inner 1840x2092 and outer 1080x2092)
  2. Run agent-device screenshot /tmp/test.png
  3. Run file /tmp/test.png → reports data instead of PNG image data
  4. Inspect with xxd /tmp/test.png | head -3 → starts with [Warning] Multip... instead of PNG magic bytes

Suggested Fix

Either:

  1. 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.

  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions