Search code examples
androidmacosadb

Screenshots with adb from script which is run form macOS application


I'm trying to take a screenshot on an Android-device/emulator, which should be triggered from within an macOS application.

I've created a bash-script - and it works well if I run it from the terminal.

However, when I run it from my macOS application, I get an error when I try to pull the image from the device.

ADB="$1"
DEVICE="$2"
PATH="$3"
FILENAME="$4"

$ADB -s "$DEVICE" shell screencap -p "/sdcard/${FILENAME}"
$ADB -s "$DEVICE" pull "/sdcard/${FILENAME}"

I get the following error:

adb: error: cannot create './EBC34F20-4624-4435-806B-15D844F4540C.png': Read-only file system

Why is it different from when I run it in the terminal? I've tried looking at which user is executing though adb and that all comes back with the same info.

I've removed sandbox and hardened runtime from my macOS app to to avoid it interfering.

It doesn't seem to matter if I change location (not /sdcard) on the emulator either.


Solution

  • I don't know what the actual problem is with the file-system on your device but I would simply avoid using the file-system at all and directly pipe the screenshot to stdout and via adb to your Mac.

    Remove the following two lines:

    $ADB -s "$DEVICE" shell screencap -p "/sdcard/${FILENAME}"
    $ADB -s "$DEVICE" pull "/sdcard/${FILENAME}"
    

    and replace them with this line:

    $ADB -s "$DEVICE" exec-out screencap -p > ${FILENAME}
    

    No adb pull and hence no problems with the file-system at all.

    The command has been taken from the following answer: https://stackoverflow.com/a/31401447/150978