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

Share and Enjoy:
  • DZone
  • Digg
  • StumbleUpon
  • NewsVine
  • del.icio.us
This entry was posted in JavaMedia. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

9 Comments

  1. Posted June 3, 2010 at 12:54 pm | Permalink

    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. Anselmo
    Posted June 19, 2010 at 8:46 pm | Permalink

    hey man, nice tutorial, thank you!

  3. Hari
    Posted July 19, 2010 at 11:10 pm | Permalink

    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?

  4. Chuasiarocker
    Posted July 30, 2010 at 6:02 am | Permalink

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

  5. Posted August 2, 2010 at 7:18 pm | Permalink

    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.

  6. Posted August 2, 2010 at 7:31 pm | Permalink

    Hello Hari,
    I haven't been able to continue the project. I'd first like to get my very own Android phone, but I keep postponing the purchase, waiting for the perfect phone :)
    In any case, I think the Android docs will be able to help you out. Take a look at http://developer.android.com/guide/topics/data/... This has most of the information you'll need.

  7. Posted August 2, 2010 at 7:47 pm | Permalink

    I think the same resource I pointed Hari to will help you too: http://developer.android.com/guide/topics/data/...
    You can put stuff on the SD card from your Android application.
    If you want to create reference data from some external program, you can use the adb tool, which is documented right here: http://developer.android.com/guide/developing/t...

  8. Posted August 2, 2010 at 7:48 pm | Permalink

    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.

  9. Abhi
    Posted August 11, 2010 at 12:43 pm | Permalink

    “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

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

blog comments powered by Disqus
  • Categories

  • Archives

  • Subscribe

    If you liked this post, why not subscribe to the RSS feed? Two times a week, you'll receive the latest post right in your news reader or mailbox.

    Subscribe via the RSS feed

    Or subscribe with your email address