Android devices have file systems similar to those of regular computers. Subject to permissions restrictions, we can transfer files from a computer to a device (connected to the computer by USB) or emulator, or from the device or emulator to the computer. We shall illustate by transfer involving a device, but file transfer involving an AVD is analogous.
Android Studio handles the transfer of files packaged up in the executable associated with your app automatically. However, one situation where you may want to transfer additional files manually from your development computer to a device is when your app expects to read a data or image resource from the device. Then you must store the resource in an appropriate place on the device. The project Playing Video gives an example of doing this. Conversely, if your app writes a file on the device, you may want to transfer this file to your development computer for examination; the project Read Your Contacts gives an example. |
In principle, one way to transfer files is to use Android Studio. With a device connected by USB, open the DDMS: Tools > Android > Android Device Monitor. Select the device in the Devices panel and then select the File Explorer tab in the right panel. This will give a file explorer display on the device with Push and Pull commands (left and right arrows at the top) that allow file transfer between development computer and device (in principle). However, for my Android Studio 2.1.2 Linux installation I find that this does not work reliably. Hence, I use one of the two methods below to move files between devices and the development computer.
You can transfer files using ADB from the command line. The general form of the required command to push a file from the computer to the device or emulator if there is only a single device or emulator running is adb push <Path to file on computer> <Desired path to file on device>. (If more than one device or emulator is running, you will have to add a flag to the adb command to select the desired one.) For example, if the file video.mp4 exists in the home directory of my Linux machine,
[guidry@m33 ~]$ adb -d push /home/guidry/video.mp4 /sdcard/download/video.mp4
would copy it with the same name to the /sdcard/download directory of an attached phone.
The -d flag targets a device, assuming it to be the only device connected. If the directory download did not exist in the sdcard directory, it would be created by the above command. For a single emulator, the flag would change to -e; if there is more than one emulator or more than one device running, you will have to specify the device serial number with a -s flag, as described in Installing on a Device. |
The corresponding command to copy a file from the device or emulator to the computer is as above, but with pull replacing push and the order of the directory paths switched. For example, on my Linux system,
[guidry@m33 ~]$ adb -d pull /sdcard/download/video.mp4 /home/guidry/video.mp4
would transfer the file /sdcard/download/video.mp4 on the phone to the home directory on my Linux system (assuming that I have appropriate read/write permissions in the two directories).
You can also use various apps to transfer files between your computer and devices. One that I find particularly useful is WiFi Explorer Pro, which costs around two dollars in the Play Store (there is also a free version with more restricted capability). When this app is started on an Android device connected to a wifi network it displays an IP address on that network. If you type this address into a browser running on a computer on the same network, it will open a standard file transfer and management graphical interface in the browser window, as illustrated in the following image,
This app resolves rather automatically the confusing dynamical linking of directories common on newer Android devices (I assume used to keep backward compatibility with older apps on newer devices that often no longer have physical SD cards). For example, although it may not be immediately obvious, /storage/emulated/0/Download is mapped to /sdcard/Download on this device. (See Playing Video and Read Your Contacts for a more extensive discussion.) As a consequence I strongly recommend using WiFi Explorer for file transfer between development computer and devices.
Last modified: July 25, 2016