tags:

views:

1849

answers:

3

I'm working on an Android application that needs to look at what images a user has stored. The problem is that if the user has the sdcard mounted via the USB cable, I can't read the list of images on the disk.

Does anyone know of a way to tell if the usb is mounted so that I could just pop up a message informing the user that it won't work?

A: 

I apologise for posting a non-Android way of doing this, hopefully someone can provide an answer using the Android API.

You could list the files on the root of the sdcard. If there is none, the sdcard is either entirely blank (unusual, but possible) or it is unmounted. If you try to create an empty file on the sdcard and it fails, it means that you were trying to create a file in the mount-point of the sdcard which would be denied due to a permissions issue so you would know the sdcard was not mounted.

Yes, I know this is ugly....

Neil Trodden
+11  A: 

If you're trying to access images on the device, the best method is to use the MediaStore content provider. Accessing it as a content provider will allow you to query the images that are present, and map content:// URLs to filepaths on the device where appropriate.

If you still need to access the SD card, the Camera application includes an ImageUtils class that checks if the SD card is mounted as follows:

static public boolean hasStorage(boolean requireWriteAccess) {
    //TODO: After fix the bug,  add "if (VERBOSE)" before logging errors.
    String state = Environment.getExternalStorageState();
    Log.v(TAG, "storage state is " + state);

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        if (requireWriteAccess) {
            boolean writable = checkFsWritable();
            Log.v(TAG, "storage writable is " + writable);
            return writable;
        } else {
            return true;
        }
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}
jargonjustin
An lo, someone provides the android api way of doing things. Vote up!
Neil Trodden
A: 

hi, I need help!!! how i can unmount sdcard from my application??

midoub
This isn't an answer. You should create a separate question for this
chrisbunney