Friday 19 September 2008

Writing to an SD Card in Android

The next task was to write GPS data to an SD card, so it can be pulled out later easily and independently of Android. This is not all quite there yet, but for now this is a "Hello world" example for writing to a card.

Essentially, writing to a file on an SD card is like all Java write operations, the only difference being that we first need to find the root of the SD card. This is done with a call to the android OS Environment:

try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File gpxfile = new File(root, "gpxfile.gpx");
FileWriter gpxwriter = new FileWriter(gpxfile);
BufferedWriter out = new BufferedWriter(gpxwriter);
out.write("Hello world");
out.close();
}
} catch (IOException e) {
Log.e(TAG, "Could not write file " + e.getMessage());
}


I would have thought that the root.canWrite() would return a false if there is no writable SD card there, but not so. I get the IOException when constructing the FileWriter.

In DDMS one can see that the /sdcard is read-only. Essentially there is not yet an SD card in the slot. First we need to create one. This is done with the
mksdcard command in tools, like this
mksdcard 512M sdcard.iso

Now we have a file containing a DOS file system (yes, it is good old FAT32). Now we need to tell the emulator to load this instead of the system card. This is done with the -sdcard option. (In eclipse go to Run->Debug Dialog and add the -sdcard to the options and restart the emulator).

Now the above code works and creates and writes a file to our virtual SD Card.

This can be read with the adb pull command:
adb pull /sdcard/gpxfile.gpx gpxfile.gpx

This has copied the file from the virtual card onto the filesystem and we can just read the content of the file with 'more'.

This was essentially an exercise in RTFM and you can read how to do this also at the official documentation.

2 comments:

Unknown said...

Thanks for the write up - This really helped. I am not sure why this information is so hard to find on the android dev. site.

Dag Rende said...

To clarify, the -sdcard option should be -sdcard