Search code examples
androidmobilesd-cardsmartphone

Android development - test for SD card or assume it's there?


My app will require external storage (SD card) in order to save the user's data. I've searched high and low, but I can't find any best practices for this.

Do I...

  1. Simply assume external storage will be present and skip adding checks and alerts? (Assumption here would be that external storage only goes missing in rare cases, and it's not worth spending a ton of time coding a solution for this. Typical users don't ever remove their SD cards, etc.)

  2. Check that external storage is mounted only when reading from or writing to it? (Users may remove their SD card at any time, but the app will still work until it comes time to access their data.)

  3. Add a listener at the application level that waits for external storage notifications and displays a modal alert app-wide? (This sounds ideal, but could also be overkill for all I know.)

Any insight will be greatly appreciated.


Solution

  • You should definitely check for it before using it. I would suggest #2; you're right, #3 does seem like overkill (I don't even know if there is a listener for that). The Google docs has this to say:

    Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might be mounted to a computer, missing, read-only, or in some other state.