Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Environment.getExternalStorageDirectory() returns a usable data folder when no sd card is mounted?

Imagine an Android device with no sd memory insterted. Only its own internal memory.

I'm not sure what Environment.getExternalStorageDirectory() returns in this case.

Null or a internal memory location valid for permanent data storage?

like image 738
Seraphim's Avatar asked Jul 11 '13 13:07

Seraphim's


3 Answers

public static File getExternalStorageDirectory()

Added in API level 1

Gets the Android external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

I think differs from device to device.

I have a Samsung Galaxy S3 Environment.getExternalStorageDirectory() returns sdCard0 which is path of internal storage memory.

More info @

http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()

I will not recommend the below. I had a discussion on this with Commonsware and the advice was not to use the below. But you can use the below for testing purposes.

String externalpath = new String();
String internalpath = new String();
    
public void getExternalMounts() {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process proc = runtime.exec("mount");
        InputStream is = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        String line;
        
        BufferedReader br = new BufferedReader(isr);
        while ((line = br.readLine()) != null) {
            if (line.contains("secure")) continue;
            if (line.contains("asec")) continue;
        
            if (line.contains("fat")) {//external card
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    externalpath = externalpath.concat("*" + columns[1] + "\n");
                }
            } 
            else if (line.contains("fuse")) {//internal storage
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    internalpath = internalpath.concat(columns[1] + "\n");
                }
            }
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    System.out.println("Path  of sd card external............"+externalpath);
    System.out.println("Path  of internal memory............"+internalpath);
}
like image 86
Raghunandan Avatar answered Oct 13 '22 02:10

Raghunandan


On cyanogenmod kernel on my phone (android 4.1), Environment.getExternalStorageDirectory() always returns the internal storage regardless of whether the SD card is inserted or not. Also there are some phones that don't support external SD cards at all (Google Nexus 4, for example).

On the whole, the result will be implementation-dependent and you cannot rely on this call retuning the internal storage or SD card. Similarly, the results of the call when SD card is not present will also be implementation-dependent (i.e. differ per device). From the point of view of your app, you treat it as a place to persist app data.

like image 38
Aleks G Avatar answered Oct 13 '22 01:10

Aleks G


It will depends on target real device. Some (like the Asus Nexus 7 or Samsung Galaxy S[1-3] will send their internal memory folder. On some other devices it can return null or empty.

I haven't been able to try it on a lot of devices, but from what I've seen it's that that method is a lot a "manufacturer dependant answer".

Officially it should return the storage that is not system one. (SDCard or internal "soldered memory" that is an addition to the internal core memory).

like image 34
BigbangRevo Avatar answered Oct 13 '22 02:10

BigbangRevo