Using ADB to capture the screen [duplicate] - android

This question already has answers here:
Read binary stdout data like screencap data from adb shell?
(19 answers)
Closed 6 years ago.
I'm trying to get a screenshot of the phone screen as fast as possible.
Currently, I am doing:
adb shell screencap -p /sdcard/screencap.png && adb pull /sdcard/screencap.png
However it is too slow and takes up to 3 seconds.
Is there any better way to do this? I intend to use this function with an unrooted phone.
Also what are the different arguments I can use for screencap?
Thanks.
EDIT (extra information): I intend to use this method to be able to get a live feed of the screen onto my pc. The current method works however it is too slow. I can't use adb shell screenrecord because I won't be able to access the video file while it is being recorded.

To save to a file on Windows, OSX and Linux
adb exec-out screencap -p > screen.png
To copy to clipboard on Linux use
adb exec-out screencap -p | xclip -t image/png

https://stackoverflow.com/a/37191719/75579 answer stopped working for me in Android 7 somehow. So I have to do it the manual way, so I want to share it.
How to install
Put this snippet of code in your ~/.bash_profile or ~/.profile file:
snap_screen() {
if [ $# -eq 0 ]
then
name="screenshot.png"
else
name="$1.png"
fi
adb shell screencap -p /sdcard/$name
adb pull /sdcard/$name
adb shell rm /sdcard/$name
curr_dir=pwd
echo "save to `pwd`/$name"
}
Run source ~/.bash_profile or source ~/.profile command,
How to use
Usage without specifying filename:
$ snap_screen
11272 KB/s (256237 bytes in 0.022s)
Saved to /Users/worker8/desktop/screenshot.png
Usage with a filename:
$ snap_screen mega_screen_capture
11272 KB/s (256237 bytes in 0.022s)
Saved to /Users/worker8/desktop/mega_screen_capture.png
Hope it helps!
** This will not work if multiple devices are plugged in

To start recording your device’s screen, run the following command:
adb shell screenrecord /sdcard/example.mp4
This command will start recording your device’s screen using the default settings and save the resulting video to a file at /sdcard/example.mp4 file on your device.
When you’re done recording, press Ctrl+C in the Command Prompt window to stop the screen recording. You can then find the screen recording file at the location you specified. Note that the screen recording is saved to your device’s internal storage, not to your computer.
The default settings are to use your device’s standard screen resolution, encode the video at a bitrate of 4Mbps, and set the maximum screen recording time to 180 seconds. For more information about the command-line options you can use, run the following command:
adb shell screenrecord --help
This works without rooting the device. Hope this helps.

You can read the binary from stdout instead of saving the png to the sdcard and then pulling it:
adb shell screencap -p | sed 's|\r$||' > screenshot.png
This should save a little time, but not much.
source: Read binary stdout data from adb shell?

Using some of the knowledge from this and a couple of other posts, I found the method that worked the best for me was to:
adb shell 'stty raw; screencap -p'
I have posted a very simple Python script on GitHub that essentially mirrors the screen of a device connected over ADB:
https://github.com/baitisj/android_screen_mirror

Sorry to tell you screencap just a simple command, only accept few arguments, but none of them can save time for you, here is the -h help output.
$ adb shell screencap -h
usage: screencap [-hp] [-d display-id] [FILENAME]
-h: this message
-p: save the file as a png.
-d: specify the display id to capture, default 0.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.
Besides the command screencap, there is another command screenshot, I don't know why screenshot was removed from Android 5.0, but it's avaiable below Android 4.4, you can check the source from here. I didn't make my comparison which is faster between these two commands, but you can give your try in your real environment and make the final decision.

Related

shell command to create files

I want to create files with different sizes on an Android device.
The one approach I have already tried was to create dummy files using fsutil and push it to devices.
Is there any way to achieve similar result with a command inside adb shell?
Yes, you can do it using dd command. I am not pretty sure it's available in your device. It works fine in my device, you can give a try.
In your host which connects with your Android device, with adb debug turn on, using the following command to create dummy file.
adb shell 'dd if=/dev/zero of=/data/local/tmp/test.img bs=4k count=800'
# check the result
adb shell ls -l /data/local/tmp/test.img
-rw------- shell shell 3276800 2017-06-21 17:33 test.img
The command above will get data from /dev/zero and output to /data/local/tmp/test.img (a public writable directory for Android device), adjust bs and count value in your situation.

Copy full disk image from Android to computer

I have a smartphone without the possibility to insert an SD-card.
I would like to make a dump of the biggest partition (cause I lost files and I'd like to use a dump to recover them).
The partition is 10GB.
I was looking for an ADB command to pull using dd but nothing...
I tried to use Carliv touch recovery with a 32GB usb key by OTG but the USB key didn't mount ... Then I couldn't use "dd" directly on the phone using Aroma file manager and a terminal emulation.
Thank you!
I don't understand why they closed a question that has already an accepted answer by linking a completely different question. Copying a file and copying a partition are 2 different things.
As said in comment, adb pull /dev/block/mmcblk0 mmcblk0.img worked for me. A "DD image" is only a binary image file of the device.
You want to copy a disk from your android device to your computer (preferably on your fastest drive) for faster and lossless analysis/recovery.
This is short step-by-step guide in windows (linux: scroll down) to achieve it using the linux tool dd intended for precise, bit-wise copies of data. Credits go to scandium on xda for the code, see his post for more details.
Prerequisites
make sure your device is rooted and busybox is installed
Windows:
install cygwin. During install, add netcat (under Net) and pv (under util-linux) packages; the standard install is located in C:\ so make sure you have enough disk space beforehand;
install adb e.g. through Android Studio. Make sure to add adb.exe executable file to the path variable to access it properly (guide).
Open two cygwin consoles/terminals (one sending data, one receiving data) and enter in one of the terminals to enter the device:
# terminal 1
adb forward tcp:5555 tcp:5555 # forward data over tcp connection
adb shell # open a connection
su # gain root access
BUSYBOX=/system/xbin/busybox # default location for most bb installers
# note: adapt the variable `BUSYBOX` to point to your install directory
# the TWRP default is `BUSYBOX=/sbin/busybox` (in case of bricked device)
Decide what partition to copy, the /dev/block/mmcblk0 partition is usually the one containing the data you typically would want.
In the following code, adapt the partition name according to 4. and quickly one after another type in terminal 1 and terminal 2:
# terminal 1
$BUSYBOX nc -l -p 5555 -e $BUSYBOX dd if=/dev/block/mmcblk0
# terminal 2
nc 127.0.0.1 5555 | pv -i 0.5 > $HOME/mmcblk0.raw
This saves the partition in the cygwin home directory (in a nutshell: it sends/receives output of dd over a tcp connection)
Look at the files / analysis
To mount the partition in Windows you can use (OSFmount).
To analyze the files I recommend Active# Undelete but there are tons of alternatives. With that program you can also directly load all partitions from the file (without mounting it, so step 5 is redundant in this case).
Guide for GNU/Linux users: install netcat and pv (step 1), use the Disks utility to analyze
Run as root:
adb root
Use dd to output content into stdout and write file on your computer:
adb shell 'dd if=/dev/block/platform/msm_sdcc.1/by-name/XXXXXX 2>/dev/null' > XXXXXX.img
Or all (see cat /proc/partitions)
adb shell 'dd if=/dev/block/mmcblk0 2>/dev/null' > mmcblk0.img

redirect adb logcat to android avd

i want to redirect the Android developer tools logcat output to a file into the virtual device from a shell command, before than running the android app test.
The command i usually use to redirect the output to a file is:
adb shell logcat -v time - f log.txt packageName:F *:E > /folder/log.txt
but it puts the log file into a computer directory (/folder/ in this case).
I want to change it with a directory in the virtual device but like above, it says the folder does not exist.
There is way to do it via shell command?
You can simply do
adb shell "logcat -v time -f /mnt/sdcard/log.txt packageName:F *:E"
to accomplish it all in one command from the host shell. You do not need the redirect when you use the -f flag, in fact the redirect would not capture anything if you have directed the output of logcat to a file rather than to stdout.
If that is not working, it is either because you are using a version of Android which mounts the external storage at some other path, or you do not have an emulated sdcard attached to your virtual device.
You can investigate either of these problems by examining the output of
adb shell mount
If you do not have an sdcard at all on your AVD, follow the emulator documentation instructions for creating and attaching one.
For testing purposes only there may be other paths than the sdcard at which you can write, particularly on an emulator where the adb shell runs as root, for example on some versions /data/local or similar.
You can try this one adb shell then #logcat>/sdcard/log.txt now i am sure about the results.you just need a command prompt window to be opened for adb shell,that's not so bad i guess.

How to empty (clear) the logcat buffer in Android [duplicate]

This question already has answers here:
How can I erase the old data from logcat?
(4 answers)
Closed 5 years ago.
How can I empty (clear) the logcat buffer in Android?
I use adb logcat from command line and pipe the output to a file, since the DDMS has a very limited buffer. At the moment, when I restart my app (after fixing bugs etc) the logcat buffer has data from the previous launch as well. Even uninstalling the app does not clear the buffer. The only way I've found so far to clear the buffer, is reboot. This is effective, but would like to know if there's an easier way.
adb logcat -c
Logcat options are documented here: http://developer.android.com/tools/help/logcat.html
The following command will clear only non-rooted buffers (main, system ..etc).
adb logcat -c
If you want to clear all the buffers (like radio, kernel..etc), Please use the following commands
adb root
adb logcat -b all -c
or
adb root
adb shell logcat -b all -c
Use the following commands to know the list of buffers that device supports
adb logcat -g
adb logcat -b all -g
adb shell logcat -b all -g
I give my solution for Mac:
With your device connected to the USB port, open a terminal and go
to the adb folder.
Write: ./adb devices
The terminal will show something like this:
List of devices attached
36ac5997 device
Take note of the serial number (36ac5997)
Write: ./adb -s 36ac5997 to connect to the device
Write: ./adb logcat
If at any time you want to clear the log, type ./adb logcat -c
For anyone coming to this question wondering how to do this in Eclipse, You can remove the displayed text from the logCat using the button provided (often has a red X on the icon)

How I can simulate "tail" command for file on the Android file system?

I have file on SD-CARD and my app using it as log file.
Is it possible through the adb to watch file with all changes in real time?
Like with tail -f /sdcard/myfile.log command.
This seems to work great for me:
adb shell "while true; do cat; sleep 1; done < /sdcard/myfile.log"
You can install busybox and then:
adb shell
tail -f /path/of/your/file
But remember that you should have root access to install busybox. If you are using the emulator check this one:
How to get root access on Android emulator?
You can do this with logcat. You can add a view that will only show log entries from your app and it will be continuously updated.
There is a great app for this: Terminal IDE. It contains many linux commands, and it does not need root access. You can install it from GooglePlay. Is is free of charge (and open source, GPLv2).
One of its best features is that it can be used through telnet. Start it on your phone, and type telnetd command. It will start a telnet daemon, which listens on port 8080 by default.
After that you can connect it from your PC, with the following command: (use cygwin on windows)
telnet 192.168.1.8 8080
You should use your phone's IP address instead of the above one. After a successful connection you will have an arbitrary sized terminal on your PC, which is capable to run tail -f command on your phone. And many others, such as bash and all of its builtin commands.
Building upon Jesse's answer, to do similar with a file within an app's private storage area:
adb shell "while true; do run-as com.yourdomain.yourapp cat /data/data/com.yourdomain.yourapp/app_flutter/yourfile.txt; sleep 5; done" | egrep -o 'sometext.{0,50}'
(This example is for a flutter app on Android, but is similar minus the app_flutter directory.)
do run-as changes the user under which the command is run to the application. By default adb shell user shouldn't have access to any files under an application's private storage area.
| egrep -o 'sometext.{0,50}' the cat command sends the file contents to STDOUT. egrep is taking the contents & searching for -o (only) sometext + 50 characters" using regex (hence egrep instead of grep).
Last Line Only
Replace cat with tail -n 1.
Add --line-buffered to egrep
adb shell "while true; do run-as com.yourdomain.yourapp tail -n 1 /data/data/com.yourdomain.yourapp/app_flutter/yourfile.txt; sleep 5; done" | egrep --line-buffered -o 'sometext.{0,50}'

Categories

Resources