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.
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.
- 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 disk, Image 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.
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.
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!
Please comment and let me know how you went. It’s always nice to see some feedback.
Major rewrite and addition of additional features, known bugs and images. Just over 1,100 words added!
- 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. ↩