Last Updated: September 29, 2021
·
25.43K
· yitsushi

Take a Photo via adb

Sometimes I want to take a photo remotely with my Android. My first idea was the awesome adb shell. I found some interesting command like am start that starts an activity. My target is the android.media.action.IMAGE_CAPTURE activity. I tried and it works.

$ adb shell
$ am start -a android.media.action.IMAGE_CAPTURE
Starting: Intent { act=android.media.action.IMAGE_CAPTURE }
$ 

Second nice command was the input keyevent that send a key event to my device. After a few clicks, developer.android.com said what I need here: android/view/KeyEvent. I need the KEYCODE_CAMERA (Constant Value: 27 (0x0000001b)) event.

$ adb shell
$ am start -a android.media.action.IMAGE_CAPTURE
Starting: Intent { act=android.media.action.IMAGE_CAPTURE }
$ input keyevent 27
$

And yes, I see a hopeful flash and a camera shot sound. Now cain it :)

adb shell "am start -a android.media.action.IMAGE_CAPTURE" && \
  sleep 1 && \
  adb shell "input keyevent 27"

The 1 second sleep is important because if you fire the KEYCODE_CAMERA before the IMAGE_CAPTURE activity starts then you got a full gray/black "photo".