I am not able to get actual path of the removable SD-card. Is there any way to get?

354 Views Asked by At

In some device, it's return actual path of the removable SD - card, but in some it returns null.

Basically, I want to store file according to user either internal storage of device or external (removable storage (micro SD- card)). This I need to be made from jelly bean to 6.0

public static String getExternalStroagePath(Context context) {
  // storage types
    final String FLAG = "mnt";
    final String SECONDARY_STORAGE = System.getenv("SECONDARY_STORAGE");
    final String EXTERNAL_STORAGE_DOCOMO = System.getenv("EXTERNAL_STORAGE_DOCOMO");
    final String EXTERNAL_SDCARD_STORAGE = System.getenv("EXTERNAL_SDCARD_STORAGE");
    final String EXTERNAL_SD_STORAGE = System.getenv("EXTERNAL_SD_STORAGE");
    final String EXTERNAL_STORAGE = System.getenv("EXTERNAL_STORAGE");

    Map<Integer, String> listEnvironmentVariableStoreSDCardRootDirectory = new HashMap<Integer, String>();

// add all storage to map
    listEnvironmentVariableStoreSDCardRootDirectory.put(0,SECONDARY_STORAGE);
    listEnvironmentVariableStoreSDCardRootDirectory.put(1,EXTERNAL_STORAGE_DOCOMO);
    listEnvironmentVariableStoreSDCardRootDirectory.put(2,EXTERNAL_SDCARD_STORAGE);
    listEnvironmentVariableStoreSDCardRootDirectory.put(3,EXTERNAL_SD_STORAGE);
    listEnvironmentVariableStoreSDCardRootDirectory.put(4, XTERNAL_STORAGE);

    File externalStorageList[] = null;
    String directory = null;
 //check version 
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.KITKAT) {
        externalStorageList = context.getExternalFilesDirs(null);
    }
    int size = listEnvironmentVariableStoreSDCardRootDirectory.size();
    for (int i = 0; i < size; i++) {
        if (externalStorageList != null && externalStorageList.length > 1 && externalStorageList[1] != null)
            directory = externalStorageList[1].getAbsolutePath();
        else
            directory = listEnvironmentVariableStoreSDCardRootDirectory.get(i);
        directory = canCreateFile(directory);
        if (directory != null && directory.length() != 0) {
            if (i == size - 1) {

                if (directory.contains(FLAG)) {
                    return directory;
                } else {
                    return null;
                }
            }
            return directory;
        }
    }
    return null;
}
0

There are 0 best solutions below