SD card image for Android emulator

With mksdcard tool (that comes in Android SDK) it’s possible to create a blank FAT32 image. After image is created it can be attached and used in emulator. Empty SD is not so useful so here will be shown several ways how to copy content to the SD image. So, let’s get down to business.

  1. Create SD card image
  2. Attach SD image to the emulator
  3. Copy content to the SD image

1. Create SD card image
Go to the tools directory in Android SDK. There should be mksdcard utility. Creating SD image is very simple the only needed parameters are size and location of output file. Here is how to create and save SD image to the /tmp directory:

# cd to the Android SDK tools directory
> cd /usr/local/android-sdk-linux/tools/

# see mksdcard help
> ./mksdcard
mksdcard: create a blank FAT32 image to be used with the Android emulator
usage: mksdcard [-l label] <size> <file>
  if <size> is a simple integer, it specifies a size in bytes
  if <size> is an integer followed by 'K', it specifies a size in KiB
  if <size> is an integer followed by 'M', it specifies a size in MiB
  if <size> is an integer followed by 'G', it specifies a size in GiB
Minimum size is 9M. The Android emulator cannot use smaller images.
Maximum size is 1099511627264 bytes, 1073741823K, 1048575M or 1023G

# create 16MB SD card image to the tmp directory
> ./mksdcard 16M /tmp/sdcard.img

2. Attach SD image to the emulator
Once SD is created, it can be used by Android emulator. Just define “-sdcard” input parameter when starting emulator from command line:

# cd to the Android SDK tools directory
> cd /usr/local/android-sdk-linux/tools/

# run emulator from command line
> /emulator-x86 -avd Android_x86 -sdcard /tmp/sdcard.img

Android_x86 is the name of my virtual device and it should be replaced with your AVD name. After emulator boot is completed, go to the “Storage” to see SD card details:

Menu -> Settings -> Storage

SD card in emulator can be unmounted, mounted and erased like in the real Android device. At this moment SD is empty and the next step is to push some content to the created image.

3a. Copy content with ADB
First lets try to copy content to the SD card with adb – Android Debug Bridge tool (adb is in platform-tool directory):

# cd to the Android SDK platform-tools directory
> cd /usr/local/android-sdk-linux/platform-tools

# push OGG file to the SD card
> ./adb push /tmp/song.ogg /mnt/sdcard
1136 KB/s (3191536 bytes in 2.742s)

If adb server is not running then error will be displayed and adb will be started automatically. Just try “adb push” again and this time file will be transfered to the SD card.

* daemon not running. starting it now on port 5037 *
* daemon started successfully *

Android will not immediately add pushed content to the media applications or change SD usage. OS should be triggered to re-scan SD content. This can be done with some applications from Google Market or with simple unmount/mount SD. It is also possible to create directory structure with adb to push files to the directory instead to the SD root:

adb shell "mkdir /mnt/sdcard/Music";
adb shell "mkdir /mnt/sdcard/Ringtones";
adb shell "mkdir /mnt/sdcard/Alarms";
adb shell "mkdir /mnt/sdcard/Notifications";
adb shell "mkdir /mnt/sdcard/Pictures";

As you can see, with adb is possible to manage SD content. Don’t forget that adb is 32-bit binary and if you are on 64-bit Linux you might get the following error:

/usr/local/android-sdk-linux/platform-tools/adb: error while loading shared libraries: cannot open shared object file: No such file or directory

Quick fix is to install 32-bit ncurses library with yum command (please see how to add 32-bit support at the beginning of Android emulator on Fedora Linux post):

> yum install ncurses-libs.i686

At the end of this method I will show short PHP script to automate copy process. Script will create directories and copy files from the current position:

// script creates directories and push content from the local disk
// SD image should be attached to the running emulator
// emulator-x86 -avd Android_x86 -sdcard /tmp/sdcard.img

// define adb command
$adb = '/usr/local/android-sdk-linux/platform-tools/adb';

// define local directory with content (Alarams, Music, Ringtones ...)
$local = '/home/dbunic/Documents/android/sdcard';

// scan local directories (exclude '.' and '..')
$dirs = array_diff(scandir($local), array('..', '.'));

// open directory loop
foreach ($dirs as $dir) {
    // test if item is directory
    if (is_dir($dir)) {
        // create directory on SD card (with the same name)
        exe("$adb shell \"mkdir /mnt/sdcard/$dir\"");
        // read directory content
        $files = scandir($dir);
        // open file loop (files inside current directory)
        foreach ($files as $fileName) {
            // set file path
            $file = "$dir/$fileName";
            // test if item is a file
            if (is_file($file)) {
                // push file to the current directory on SD card
                exe("$adb push \"$file\" \"/mnt/sdcard/$dir\"");

// print end execute system command
function exe ($command) {
    print "$command\n";

3b. Copy content from Eclipse
It is also possible to manage files on SD card with Eclipse in both directions (push and pull files). Start Eclipse and navigate to the Android File Explorer:

Window -> Open Perspective -> Other -> DDMS -> File Explorer (tab)

After DDMS perspective is opened and File Explorer tab is selected, directory structure of emulated Android device should be listed in window. Expand “mnt” directory and then expand “sdcard” directory. This is SD card content. To push file just mark “sdcard” (click on it to stay highlighted) and in upper right corner click on “Push a file onto the device” icon. From this point it is only needed to select a file from the local content popup. It’s also possible to pull file from the SD image to the local directory (with clicking on the next near icon).

3c. Mount SD image and copy with file manager
This is the most simple method. Created SD image can be mounted to the file-system and easily managed with the file manager. In Fedora Core 17 I had a problem with Nautilus because it claims that attached SD image is “read-only” file-system. On the other hand, deleting files from mounted SD was working just fine as copy/delete from command line. This sounds like a bug in Nautilus 3.4.2 and it will be probably fixed in newer releases. Anyway, SD image will be mounted as a root so uid and gid parameters should be set to allow “normal” user (nonroot) to have write priviliges. OK, lets see how to print user uid / gid to the terminal:

> id
uid=1000(dbunic) gid=1000(dbunic) groups=1000(dbunic)

Go to the /tmp directory and create mount point as “normal” user:

> cd /tmp
> mkdir mnt

su to root and mount SD card:

> mount -t vfat -o rw,uid=1000,gid=1000 sdcard.img mnt

Now simply copy files to the /tmp/mnt and “eject” card when content arrangement is finished

> umount /tmp/mnt

2 thoughts on “SD card image for Android emulator”

  1. hello, I tried your steps in ubuntu, after copying files to /tmp/mnt I tried to umount but I always get: umount: /tmp/mnt: device is busy. I appreciate any suggestions about how to proceed with this.

Leave a Comment