Kirk
Kirk

Reputation: 5077

Android Internal External Storage

I am on developing an App which has backup functionality.I have implemented the functionality successfully but the only one area i am still having trouble is the following...

To check the External Storage availability we can use following function to make sure that the SD card is presented.

Some android model phones have only internal memory. in such cases how am i suppose to handle the issue.

public static boolean isExternalStorageWritable(){
    String state = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(state)){
        return true;
    }
    return false;
}

public static boolean isExternalStorageReadable(){
    String state = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
        return true;
    }
    return false;
} 

Also i am aware that this will return the location of DCIM folder.but i don't think it is a best place to store important backup files.

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

Did anyone already pioneered in this problem?

Upvotes: 1

Views: 1327

Answers (2)

AnV
AnV

Reputation: 2844

Both Environment.getExternalStorageState() and Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) will not return paths to SD card.

Before API level 19, there was no official way to store in SD card. But, many could do it using unofficial APIs.

Officially, one method was introduced in Context class in API level 19 (Android version 4.4 - Kitkat).

File[] getExternalFilesDirs (String type)

It returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

That means, it will return paths to both Micro SD card and Internal memory. Generally, second returned path would be storage path of micro SD card.

To check the External Storage availability we can use following function to make sure that the SD card is presented.

To check whether SD card is available, you need to use getExternalStorageState(File)

Official Android reference documentation says,

Shared storage may not always be available, since removable media can be ejected by the user. Media state can be checked using getExternalStorageState(File).

For more details visit this link : getExternalFilesDirs(java.lang.String)

you may think that, if both above mentioned methods will not return paths to SD card, then why is there a term External present in those method names.

Because, according to Android Official Docs/Guide External Storage means, both internal memory and microSD card Storage.

In its own words,

External Storage can be a removable storage media (such as an SD card) or an internal (non-removable) storage.

The Internal and External Storage terminology according to Google/official Android docs is quite different from what we think.

Upvotes: 0

danny117
danny117

Reputation: 5651

public class CheckStorage {
    /* Checks if external storage is available for read and write */
    static public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    static public boolean isExternalStorageReadable() {
        if (CheckStorage.isExternalStorageWritable()) {
            return true;
        }
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }
}

Upvotes: 1

Related Questions