开发者

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开发者_StackOverflow中文版 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.


Yes u have to test for the presence of the sdcard the following function would help you.

private boolean checkForDirectory()
     {
        boolean cardstate = true;

         String state = Environment.getExternalStorageState();

            if (Environment.MEDIA_BAD_REMOVAL.equals(state)) {
                cardstate = false;
                runDialog("Memory Card was removed before it was unmounted");
            }
            else if (Environment.MEDIA_CHECKING.equals(state)) {
                runDialog("Memory Card is present and being disk-checked");
            }
            else if (Environment.MEDIA_MOUNTED.equals(state)) {
                 cardstate = true;
                //runDialog("Memory Card is present and mounted with read/write access");
            }
            else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
                runDialog("Memory Card is present and mounted with readonly access");
            }
            else if (Environment.MEDIA_NOFS.equals(state)) {
                cardstate = false;
                runDialog("Memory Card is present but is blank or using unsupported file system");
            }
            else if (Environment.MEDIA_REMOVED.equals(state)) {
                cardstate = false;
                runDialog("Memory Card is not present");
            }
            else if (Environment.MEDIA_SHARED.equals(state)) {
                cardstate = false;
                runDialog("Memory Card is present but shared via USB mass storage");
            }
            else if (Environment.MEDIA_UNMOUNTABLE.equals(state)) {
                cardstate = false;
                runDialog("Memory Card is present but cannot be mounted");
            }
            else if (Environment.MEDIA_UNMOUNTED.equals(state)) {
                cardstate = false;
                runDialog("Memory Card is present but not mounted");
            }

            File dir = new File(Environment.getExternalStorageDirectory() + "/gwc");
            if(dir.exists() && dir.isDirectory()) 
            {
               System.out.println("Folder exists");
            }
            else
            {               
                String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
                File myNewFolder = new File(extStorageDirectory + "/gwc");
                myNewFolder.mkdir();
                System.out.println("Folder gwc created ");
            }
            return cardstate;
     }


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.


Absolutely agree with Shawn Lauzon's answer, and here is a post on developer.android.com, that has some code for checking if external storage is available and if it's writable. Hope this helps you.

Data Storage

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜