Transfer and Delete files on Android device using ADB

ADB(Android Debug Bridge) is a great asset for anyone writing android automation scripts. Some test cases require data in the form of images, documents, mp3 file etc to be present in the android device/emulator and ADB gives simple yet powerful utility to do that.

GETTING STARTED

  1. ADB is present in the Android SDK Platform-Tools package. I hope you have platform-tools installed on your system. To verify its version you can use command:
    $ adb version
    Android Debug Bridge version 1.0.41
    Version 29.0.1–5644136
    Installed as /Users/<username>/Library/Android/sdk/platform-tools/adb
    In case you have not, please go through this.
  2. Your android device connected to your system with developer options enabled. Also enable USB Debugging through developer options → USB Debugging.
  3. Verify that device is detected by the ADB using $ adb devices. Output should be like:

Push files to Android Device

Before pushing the files to the directory on your device’s internal and external storage, you will require to locate the desired directory on your device. This can be checked by using

$ adb shell

This starts an interactive shell and you can traverse through different directories to reach your desired location of file transfer. Before using next set of commands either exit shell or use new terminal tab.

Use the directory location to push files from your system using

$ adb push <path/to/file/<filename>> <path/to/desired directory>

Ex: $ adb push /Users/…../image.jpeg /sdcard/…/targettedFolder

Adding steps to use the same command for JAVA.

String adbCommand = "adb push <path/to/file/<filename>> <path/to/desired directory>";Process process = Runtime.getRuntime().exec(adbCommand);
printResults(process);
public static void printResults(Process process) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}

Output of the above command:

So far so good. File transferred to the device but wait, it may be not visible in your targeted directory. Although you can check if it is actually transferred using adb shell command and traversing to directory. It will show the file is actually present. But why it is not visible on UI??

Possible reason could be that your android device did not fire required task to update the state of its file system. For this, either you can reboot your device or you can explicitly fire command:

$ adb -d shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:<path to file transferred>

Ex: adb -d shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:/sdcard/DCIM/Image.jpeg

Same command would not work if you are using an emulator and you can get error message

To fix this, use command:

$ adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:<path to file transferred>

Adding steps to use the same command for JAVA.

String adbCommand ="adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:<path to file transferred>";Process process = Runtime.getRuntime().exec(adbCommand);
printResults(process1);

Delete Files from Android Device

If you require to remove a single file from android device use the command:

$ adb shell rm -f <path to file>

Ex: adb shell rm -f /sdcard/DCIM/Image.jpeg

If you require to remove multiple files with same prefixes use the command:

$ adb shell rm -f /sdcard/DCIM/<common prefix>*.jpeg

Ex: adb shell rm -f /sdcard/DCIM/Image_*.jpeg

Same command will work for emulators as well.

You may again have to explicitly tell android device to rescan the directory.

$ adb -d shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:/sdcard/DCIM/

Pull files from Android Device

$ adb pull /sdcard/DCIM/Image.jpeg

Above command pulls the image from the device to the root of your system. If you want to transfer the file to a specific directory then use command:

$ adb pull /sdcard/DCIM/Image.jpeg <path to directory>

On successful transfer you will see message like this:

It is a simple utility and add lot of value to your test automation suite. I hope this post added value to your already growing knowledge pool. Will keep posting more such stuff in future. Take care. Keep Learning. Stay SAFE.

Automation Enthusiast | Ex-Thoughtworker

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store