I want to let the user safely开发者_如何学C remove the SD card in my program, but it seems the Android 2.2 public API does not provide a way to do this. Does anyone know what's a proper way to do it?
You need to take the user to the device's built-in Settings. I think this will work.
Intent i = new Intent(android.provider.Settings.ACTION_MEMORY_CARD_SETTINGS);
startActivity(i);
Unmounting the SD card is one of those actions which could be used maliciously if it wasn't under full user control. If it could be done purely in software (without user intervention) then code could disrupt other apps running on the device.
Indeed on the Logitech Revue (the Google TV companion box) that Intent call launches the Storage control panel which gives user access to the Unmount command for safe removal. If you just pull the USB device out on that unit it is experienced as an Unsafe/unexpected removal and your card could be left in proverbial disarray if treated that way. Thanks for the hint, very useful.
The Revue by the way have an unusual arrangement with the /sdcard unit built in, so if you plug in an external SD card via USB adapter or use some USB stick it will show up under /mnt/ as a name beginning with "usb" and some number. So you have three storage areas on that unit, internal, the built in "sdcard" and USB ports where drives will mount when inserted.
I haven't earned enough points yet to be allowed to include a snapshot of how the panel looks.
精彩评论