Coping with losing SD card access while app is running
My bitmap editor app 开发者_运维百科uses the SD card to store application data where e.g. I autosave the document state to the SD card every 30 seconds. I know that when the user plugs their phone into their computer, the SD card cannot be accessed by applications any more if the SD card gets mounted on the computer. I need to make sure my application behaves predictably when this happens. I have a couple of queries:
What happens if I'm in the middle of a function call that is writing to a file when the phone is plugged in? Does Android give me a chance to finish up writing to the file or does the transfer get interrupted?
Is there any way to get a warning that SD card access is about to be lost to give my app a chance to save its state to disk?
Android uses intents to broadcast actions across the system; in this case with ACTION_MEDIA_EJECT. This should give your app warning to save it's state to disk.
What happens if you are in a middle of a function call? well that depends how you implemented your function; and can range from silently failing to save file, to crashing the app
精彩评论