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

23 thoughts on “Android Tutorial: Creating and Using an SD Card in the Emulator”

  1. Hey nice tutorial but i have some problem :-
    I have made the sdcard
    But i can not use the buttons on the DDMS File explorer they are turned grey and i can not click on them
    Plus i want to ask is this the only way to add videos to my project??
    I want some videos embedded in my project which will play on specific activities

  2. Hi Peter,
    Very nice tutorial. Could you please point me the follow post to this one where you show us how to access and manipulate the files from CD CARD?

  3. Is this the only way to add images into the sdcard? Is there like a coded kind to add in images?

  4. It depends, if the video is part of your project, you can probably at it as some kind of resource. Or, maybe even better, you can load it from an url when you need it.
    If, on the other hand, you want the play video that is added by the user, I think the SD card will be your best bet.

  5. Thank you for the kind comment. I plan to create more Android tutorials in the future. It's just that a few other projects have currently taken over my spare time.
    But keep checking back, there will be more mobile stuff.

  6. “Failed to push 0007.jpg on emulator-5554: Read-only file system”

    This is what I get on adding the image file..I want to know how to change the permission to read and write..thx

  7. I am a new Android developer. Excellent tutorial. I was able to push images to the sdcard after reading this.

  8. Hey Abhi, I’m not sure if I will be of much help. One thing I can think of is to make sure you are writing to the sdcard. Some other parts of the filesystem might be readonly.

  9. Sir I want push a xml file which is in Tomcat Server to sdcard Emulator device to retrieve and parse the xml file.Pls help me to fix this issue

  10. Hi I am also getting an error while upload a small jpg file.
    Failed to push selection: Read-only file system
    I noticed in the File Explorer the sdcard is having permissions as d——
    no permissions to update the sdcard folder. Kindly advise.
    SD

  11. This tutorial is fairly old by now and I’m not sure if I’ll be able to update it in the near future.
    You might want to check if you’re using the correct folder (make sure it’s the SD card) and if there is some kind of recently added options to switch the read-only mode on SD cards.

  12. I tried this tutorial, and got read only errors too. I found an easier way is to edit your AVD to have an SD card (in AVD manager, edit your AVD and specify a size for the SD card). Kill and relaunch the emulator, and the DDMS view will now let you push files properly. Also, if you use this approach, don’t use the -sdcard command line option anymore.

  13. I did exactly as the tutorial says and yet I can’t see the sdcard in DDMS … I’ve the latest android sdk-version-10 and i spent a day trying to insert the sdcard to the emulator. Any help?

  14. Did you add the command line option? (step 2)
    Are you sure the path is correct? What is the working directory of the emulator executed?
    Are you seeing any error messages on startup?
    I think answering those questions should help you get a little further.
    Good luck!

Comments are closed.