Friday, June 17, 2011

How to mount an sdcard in the Android Emulator

Ahoy ahoy,

I've been working on an Android application. The idea was pretty simple enough: an alarm that plays a random song each time it goes off. It being my first venture into Android and developing with Java (no, I was not using Monodroid to do it in .NET... at least not yet), I was bound to run into a few hurdles along the way.

One of these problems I ran into was how to make the emulator think that it has an sdcard installed so that I could query it for all the music that it contains. There are a few articles spread out on blogs and IT sites on the topic but I found nothing that was comprehensive and complete enough to allow me to learn the basics on it and get it done quickly. The goal of this article is just that.

I create a test project you can run in Eclipse to verify that you've indeed got your SD card properly installed on the emulator. It is simple enough: a ListActivity that lists the title of all songs found on the SD card.

You can download it HERE!

1. Create an SD card
The android SDK comes with a tool to create an SD card file to be used with the emulator. The tool is mksdcard. To use the tool:
  • open a command prompt (Windows Key + R, type cmd, hit Enter)
  • go to the directory where the android sdk was installed (mine is C:\Java\android-sdk-windows\tools)
  • mksdcard 512M C:/Users/Sean/Workspace/emulator_sdcard
  • Wait a few seconds (the bigger the sd card size, the much longer you'll wait...)
  • Verify that the file was indeed created in the desired location

2. Assign the SD card to the emulator
This is done inside of Eclipse.
  • In the top menu, go to Run > Debug Configurations...
  • Choose your debug configuration in the treeview on the left. You may have to create one if you have not debugged the project before.
  • Select the Target tab.
  • At the bottom of the options is a field. Additional Emulator Command Line Options
  • Enter -sdcard your_sd_card_location (mine was C:/Users/Sean/Workspace/emulator_sdcard)
  • Hit the Apply button at the bottom. Then close the dialog.
  • If the emulator is already open, close it.
  • Now debug your project. (Right-click on the project > Debug As > Android Application

If we re-run our test application, there is still nothing of interest. We must now fill the SD card with useful data in order for it to be read by our killer app.

3. Push data onto the SD card
To accomplish this we will use a graphical tool included in the android sdk and found in Eclipse. If you stopped the debugger from the previous step, go ahead and relaunch it.
  • In Eclipse from the main menu, go to Window > Show View> File Explorer. Or you could choose the DDMS View already inside Eclipse. It contains the File Explorer.
  • Once inside the File Explorer, you'll find the "Push a file onto the device" button in the menu bar. But beware, you can't add your data files just anywhere on the SD card. Many system directories are read-only.
  • Go into the mnt/sdcard folder and add your data there. You can do so by:
- The "Push a file onto the device" menu item
- Drag and drop the file onto the selected folder
- Through adb (the command-line tool) with the following command:
adb push [C:\music\example.mp3] /sdcard/[example.mp3]
adb push [C:\music\myFavoriteAlbumDirectory] /sdcard/[directoryName]
(adb is located in the android-sdk installation directory. Mine was C:\Java\android- sdk-windows\tools)

You should now see your file on the SD card in the file explorer.

Hey, wait a minute! It's not there, something went wrong.
It's true, we don't live in a perfect world and yes, things can go askew sometimes. If your file is not there, look in the output console in Eclipse, it can help us find out why. Some possible causes/remedies:
  • You tried adding your file in a read-only directory. Push the file into mnt/sdcard to be sure.
  • You may need to restart Eclipse (it worked for me when I got this message in the console:
[2011-06-18 10:30:17] Failed to push 07. That's The Way Love Is.mp3 on emulator-5554: null
  • Your SD card is full. Unfortunately if this is the case, you cannot resize the SD card file you created initially. You'll have to recreate it with mksdcard but with a bigger size this time.
If you want to create a directory structure on the SD card, you cannot use the File Explorer in Eclipse. Instead use a file browser app on the emulator or adb (same command used to push a file but use it on a directory instead... but not an empty directory... that won't work).

4. Relaunch the app
At this point we will see our list of music on the sdcard in our app. Close the emulator and debug the application once more. We should now see a list of the songs (we obtained through legal means...) we added to the SD card.



  1. permissions at mnt/sdcard d---------
    how to change that permissions? I can't push file to emulator me please.thanks

  2. hey tika, do you have solved the problem because I have the same?