Tutorial: Read/Write Android Emulator sdcard.img in Windows

For developers working with the Android Emulator on Windows, it can be quite tedious to interact with the “microSD card” (read:  image) from outside the Android emulator.

In order to access files and folders within the image in Windows, there are currently two methods of doing so (that I am aware of): by accessing the image through the Dalvik Debug Monitor Server, also known as DDMS for short, whilst the Android Emulator is running or by command line through the Android SDK, again, whilst the emulator is running.

Microsoft Windows will not mount such images nativelyloop device”. Seems like Windows is missing out on the party. ;-)

Despite this limitation, it is possible to treat Android Emulator images as physical disks. This is made possible through a tool called “ImDisk Virtual Disk Driver”, which as it can be extrapolated, allows virtual disks to be mounted and used like physical ones. It’s fast(er than DDMS, at least for me) and user friendly, since any developer worth their salt knows their way around Windows Explorer (or at least they should ;-)). This tutorial has been written to highlight that this is possible and also discusses issues surrounding the implementation of such a system as well as quirks that I have come across whilst developing the tutorial. This tutorial would also be applicable for other files intended to be “virtual disks”, though this has been written for use with the Android Emulator [micro]SD card image and as such, that discussion is beyond the scope of this article.

Whilst DDMS offers a visual interface to do such tasks as reading and writing files through its “File Explorer”, I’ve found it to be slow and to be lacking in features (certainly in comparison to Windows Explorer). I’ll admit that I’ve not used the command line for such tasks as copying files to and from the image but it is possible (I tend to avoid it if I can), though being the command line, it’s not exactly user friendly unless you’re a DOS junkie. ;-)

Obligatory disclaimer
I have not tested this concept thoroughly in the sense that it would be ‘commercially supported and warranted’ (nor do I warrant it, use at your own risk, type of thing) and as such I take no responsibility if you somehow manage to destroy your emulator, some or all of the data on your virtual cards, your computer, your house or any other type or form of misadventure by following this tutorial, in part or in full, so be warned. ;-)

This tutorial is subject to mistakes, limitations in my knowledge of computer systems (I’m not an IT know-it-all ;-)) and oversights, so if you see something that should be corrected, don’t hesitate to comment. :-)

I’d suggest regularly backing up the images you wish to use with this procedure, to guard against any data loss or failure. It would be a good idea to create another emulator based on the version of Android you want to eventually use this procedure for, and test any operation you want to perform before using it on a development or production system. But as always, back it up!

Now that we have that out of the way…

I’ve used the following system configuration, which should work on other Windows systems and Android builds:

  • Windows 7 Ultimate x64 (not running in Test Mode!! :-D)
  • Android SDK Emulator; Android 2.1 and 2.2, API Level 7 and 8 virtual devices
  • ImDisk Virtual Disk Driver v1.2.8

ImDisk is what makes this all work and which is unlike Alcohol 120% or Daemon Tools which (as far as I’m aware) do not allow writing to virtual images as they were designed for non-writable optical media.

Caveats

  • You can’t have the image mounted and running in the emulator at the same time. Either ImDisk will complain that the file is in use, or if already mounted, the emulator will not boot into Android. So in order to copy or get anything from the image, you’ll have to close the emulator to mount the image or unmount the image to run the emulator. This appears to be file locking behaviour in Windows, to prevent corruption.
  • Windows User Account Control (UAC) can interfere with ImDisk (specifically the drive mounting and dismounting process), if enabled. This is due to ImDisk utilising device drivers to create virtual disks. UAC may even interfere if your profile is granted administrative privileges. However there is a work around for this, apart from beheading disabling UAC.

How to mount virtual disk image files in Windows

This will require administrator privileges to set up. It should be possible to run without administrator privileges once installed though, according to ImDisk (handy for those in secured computing environments), though this is a claim that ImDisk makes.

1. Download and install ImDisk Virtual Disk Driver. Those using 64-bit Windows are able to use ImDisk, as its drivers are digitally signed so Windows won’t have a fit. :-)

2. Go to Control Panel (ignore ImDisk Virtual Disk Driver at this stage)> Administrative Tools > Services and locate the ImDisk Virtual Disk Driver service. Go to its [p]roperties, set it to start Automatic[ally] and start it. Reboot. This is so reduced privilege users are able to use it, or should be able to use it (ultimately depending on the environment it’s installed in).

3. Locate where the [micro]SD card image (named sdcard.img) is located that is used by the Android Emulator. Typically this will be buried in the local user profile area of:

C:Documents and Settings[username].androidavd (Anything earlier Vista)

- or -

C:Users[username].androidavd (Vista and above).

4. Right-click on sdcard.img and select Mount as ImDisk virtual disk. If this option is missing, follow the instructions in step 5.

ImDisk will pop up and present you with a configuration dialog. Leave Size of virtual diskImage file offset and Device type as their default values. Selecting Removable media and Read-only media is optional. I’ve used these two features tentatively and it doesn’t seem to affect the whole scheme of things and I have yet to examine their effect in depth… Unfortunately, that doesn’t mean that it doesn’t affect things. Just be aware of the enabling or disabling of these two settings if something goes awry.

Don’t check Copy image file to memory unless you do not want to save any changes you made in Windows Explorer. This feature uses a non-persistent copy of the image and doesn’t modify the original copy or save the non-persistent copy automatically when the virtual disk is dismounted. This is covered in 2.1 of the unmounting section.

You may be asked to select a partition when mounting the image, just select Use entire image file.

 

5. If you get an Access Denied error in versions of Windows secured by User Account Control (UAC), check the NTFS permissions of sdcard.img, making sure you (and System) has rights to access the image, and either disable UAC (I don’t recommend this considering UAC is useful, despite being irritating at times), or, find the ImDisk control panel applet (typically C:WindowsSystem32imdisk.cpl), create a shortcut to this applet on your desktop (Send to > Desktop (create shortcut)), set the shortcut to run under administrator privileges (Right-click > Properties > Advanced > Run as administrator) and use this shortcut to mount the image by opening the control panel applet directly. This method also allows you to mount images if the “Mount as ImDisk Virtual Disk” item is missing, by clicking “Mount new…”, then clicking the ellipsis button to select the image. Continue on from step 4 if you were sent here from step 4.

6. If ImDisk complains of the file being in use, close the emulator. They won’t run together. Unmounting the image in Android does not allow the image to be mounted in Windows, the emulator must be shut down in order to free the image from its locked state.

7. If the emulator won’t run or boot, unmount the image using ImDisk. If ImDisk complains that the image is in use, you can generally forcefully unmount it (providing you aren’t knowingly accessing it) without it becoming corrupted. Back it up before doing so, just in case. This is the file locking behaviour of Windows working, which I don’t believe can be circumvented.

8. Enjoy accessing files and folders onto the virtual SD card image in Windows! It certainly helps to be able to do this, especially if you don’t like wrangling with the command line or have a lot of data to copy onto the card or off the card.

How to unmount virtual disk image files in Windows

1. Make sure you have finished accessing the mounted image and close all Windows Explorer windows viewing the disk.

2. In Computer (or My Computer if you’re using a version of Windows preceding Vista), right click on the virtual disk and select Unmount ImDisk Virtual Disk. You may be prompted that the disk cannot be automatically unmounted (as something may still be accessing it) and requests you to forcefully unmount it. You can generally forcefully unmount it if you aren’t knowingly accessing the image. If the disk is still actually being accessed, it may result in data loss or corruption.

You can also unmount disks through the ImDisk control panel, by selecting the disk and then clicking Unmount at the bottom of the applet.

2.1. If you’ve set up the virtual disk with the Copy image file to memory feature, right clicking the virtual disk in Computer and selecting Save disk contents as image file allows you to commit the changes to another image file. You can’t save the changes to the same image file as the image is locked by Windows, and you will have to delete and rename the new image if you wish to boot Android with it.

You can also commit disk changes through the ImDisk control panel, by selecting the disk and then clicking Save image… at the bottom of the applet.

Virtual disk image operations

ImDisk has two features that work with Android emulator [micro]SD card images, those being formatting and expanding the virtual disk size. The following features were tested on an Android 2.2 build with Google API’s and under administrative privileges.

Formatting the [micro]SD card image

If for some reason you want to format the image but don’t want to boot into Android to do so, this method does work and appears to work quite well.

From within the ImDisk control panel, select the disk (read: mounted image) you wish to format then select Format… This will bring up the Windows formatting tool. Do not format the card as NTFS, Android does not support this file system and will not mount the image in the Android emulator, even if the emulator boots.

Set the desired allocation size, although the one Windows suggests or Default allocation size is recommended. I’ve tested 4096 bytes which works quite well (and seems to boost performance in the emulator).

It doesn’t appear to matter whether Quick format is enabled or not, at least in my travels with this. It may be worthwhile disabling this if you need the format to deeply clean the image, for whatever reason.

Clicking Start will bring up the obligatory: your data will be destroyed notice. Click OK to sign your life away. Once the format completes, dismount the image and boot the Android emulator.

Expanding the [micro]SD card image size

If your virtual image is quickly running out of space, this can be an extremely feature to have since the SDK does not allow you to change the size once the image has been created, although it does have one caveat: you must format the image to take advantage of the increase in size.

1. Mount the virtual image you wish to expand and backup its contents. Once you’ve done this, select “Extend size…”. You will be prompted with a dialog asking you to enter the amount of space you want to add onto the current image size, it does not ask you to enter the total size of the new disk. In testing, I added 500MB and 1GB in separate blocks.

2. Once that has completed, you will be prompted that the operation was successful, but ImDisk was unable to expand the file system, which requires a format.

3. Format the image. You should now have a working image with additional space. If you’ve backed up the contents, copy the data back to the expanded image.

Important notes

Nothing noteworthy so far.

Known bugs and quirks

- There is a possible conflict between this method and the use of the MediaStore class in the Android SDK, where files can’t be accessed by MediaStore. I am currently looking into this. Thanks Padmini!

- Mounting the image in ImDisk (where you have not been prompted for privilege elevation (UAC popping up)), then subsequently attempting to dismount the image with privilege elevation will fail and present the following error:

(For the benefit of Google and screen readers, the error is: “Not an ImDisk Virtual Disk: ‘[drive letter]:’”), where [drive letter] is the letter assigned to the ImDisk virtual drive). This appears to be an ImDisk related issue and which I have reported to the ImDisk developer.

Concluding remarks

I hope this helps you in your Android development adventures as I’ve been looking around for a better solution to the original problem posed above and managed to come up with this way to make working with the emulator easier. It also gets irritating that Linux and Mac users can mount images natively through their respective loop devices. Equality FTW! 8-)

Please comment and let me know how you went. It’s always nice to see some feedback. :mrgreen:

Cheers!


Updates

2010/12/14

Major rewrite and addition of additional features, known bugs and images. Just over 1,100 words added!


  1. At least before Windows 7. A utility called ‘diskpart’ can allegedly do this, though it is unclear at this stage whether it works with Android (micro)SD card images. I will get around to testing this soon.
This entry was posted in Technical, Tutorial and tagged , , , , , , , , . Bookmark the permalink.

23 Responses to Tutorial: Read/Write Android Emulator sdcard.img in Windows

  1. Pingback: Market Workaround

  2. Adrian says:

    Thank you for this great tutorial!  You really saved me a lot of time. :)

  3. Padmini says:

    Thank you very much for the invaluable information provided. I was struggling for the past 2 days to copy the image files to the img file. Once again, thank you.

    I can copy the files to the sdcard.img, but from the android development, when I am running the program, i get 'cannot open the sdcard,img'. can you tell me why…so?

    Thanks in advance.

     

  4. Padmini says:

    You are right, I unmounted the img file after copying the image files to it. Android is able to open it. But, I am stuck with one more problem. I am using the MediaStore to get the image files, which I have copied to the sdcard.img. The Cursor is null and I am getting the Runtime Exception , Force close.

    Also, in the File Explorer in DDMS, I am not able to see the image files listing which I have copied. Of the 3 folders , i can see in the File Explorer [data,mnt and system], there is a subdirectory sdcard in the mnt folder, but it is empty.

    I am going wrong somewhere, Thanks in advance.

     

     

  5. Naveed Momin says:

    Thanx a lot for the valuable information, i am novice to android development and sisnce last 3 days i m trying to add files in the sdcard, finally your tutorial helped me to solve add media files.
    Thanx a lot again.

  6. Doesn't work for me – 'There was a problem starting imdisk.cpl, The specified module cannot be found'

    I tried rebooting, verifying the service is running, but no luck.

  7. Jim Shimota says:

    This worked like a charm.  Actually IMDisk solves some other problems, like writable ISO's etc… so nice call!  The steps to dismount the img were a bit foggy but the CPL file tipped me off and I found the IMdisk Virtual Disk icon in my Win 7 control panel.  Files copied from Win7 explorer errorless.  I dig get the file in use error when attempting to dismount, but it offered to force, and that worked fine.  Thx!

    • deltafalcon says:

      Thanks! I'll improve the tutorial in the coming days, clarifying unmounting the image procedures. I'll get some screenshots happening as well.

  8. Dax says:

    Thank you very much. it work for me.

  9. kasur says:

    Thank you for such detailed description.

  10. ifhayz says:

    Thank you, this is really good tutorial.. :)

  11. Paul says:

    Thanks Deltafalcon, this was really helpful. It has made life a little easier while trying to get into developing for Android with Windows. Android sdk pages should link to this page.

  12. Sandra says:

    Excellent tutorial. Thank you very much indeed!

  13. Brian says:

    Thanks mate, I was looking for just this exact thing and you have saved me loads of time. Still baffles me why the Android SDK wouldn't have a much simpler image management system!

  14. kashif says:

    Greatttttttttttttttttttttttttt…… that was exactly what i was looking for… :)

  15. Pappsegull, Sweden says:

    Great work! Very nice tool and tutorial, just what i was searching for!

    Do you have any idea of how to open the asec files (E:.android_securemy.app.asec) ?

    /Thanks

  16. qqaadir says:

    Dear Sir,
    I appreciate your post its marvelous ,But as i am a beginner to android i am getting problems.My code is not running
    showing problem like 5554 emulator disconnected and disk partition is already in use.I have installed imdisk and not finding the file sdcard.img in (C:Documents and Settingsali.androidavd).
    Kindly guide me as I am trying for two days.
    regards.

  17. Pingback: How to access files in AVD SD card? - Android Forums

  18. scrajesh says:

    Nice tutorial..

    got over the image upload to the sdcard.img issue…

    but after doing so, my app isn't installed in the emulator.. the log shows the following line repeatedly:
    Device is not ready. Waiting for 20 sec.
    DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.package.example"

    any work-around the above..?????? kindly help if possible..

  19. Stephen says:

    Much appreciated tutorial and very concise! Thanks loads!!!

Leave a Reply

Your email address will not be published. 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>