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

£可爱£侵袭症+ 提交于 2019-12-09 16:18:11

问题


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?


回答1:


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);
}



回答2:


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.




回答3:


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).



来源:https://stackoverflow.com/questions/17594212/does-environment-getexternalstoragedirectory-returns-a-usable-data-folder-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!