Android Tutorial: Creating and Using an SD Card in the Emulator

In a previous post, I mentioned how user-friendly Android is for developers. There’s extensive documentation and information on the architecture, the different components and best practices. However, the documentation has a few blind spots. Many of the tools have little documentation and no usage examples. The information is there, it’s just hidden behind a few layers of Android theory.

So I’m going to try and document a few of the basic things that you’ll probably need to know to start developing Android applications. Only for beginners and intermediate Android developers.

The first issue I ran into was trying to load files into the Android emulator. For instance, if you want to load MP3 files on a “real” Android phone, you’ll probably insert an SD card into the phone. The emulator can emulate this, but you need to jump through a few hoops:

1.

** First, you will need to create an SD card image file. This is comparable to, for instance, an ISO image of a CD-ROM. Creating an image is done via the mksdcard tool. For instance:

mksdcard -l mysdcard 128M sdcard.img

2.

If you want to access the image, the emulator must be running with the image loaded. I’m going to assume you’re using the Eclipse tools. In there you need to specify the image on the command line. Open the “Run configurations…” dialogue and configure the image as follows (don’t forget to put in the correct directory)

android_sdcard_commandline_config

3.

Next start the emulator and verify that the SD card is loaded. The best way to do this is in the DDMS tool. To access it: click on the open perspective button:

android_ddms_perspective

If the DDMS isn’t in the list, click “Other…” and choose it from there. Open the file explorer and you should be able to open the sdcard:

android_file_explorer

4.

Add files to the sdcard: There are two options: either you can use the two icons in the top right corner of the DDMS tools (android_pull_push) or you can use the command line tools. The following command pushes the file to your sdcard:

adb push myfile.mp3 /sdcard/

Not that the location on the Android phone (the “/sdcard/” part) needs to have “unix-style” forward slashes, so keep that in mind if you’re running on Windows.

5.

And that’s it. the file should appear in the DDMS file explorer. Next time, we’ll try to actually do something with the file we places on the Android emulator.

Image credit