4

在某些设备中,它返回可移动 SD 卡的实际路径,但在某些设备中,它返回 null。

基本上,我想根据用户存储设备的内部存储或外部(可移动存储(微型 SD 卡))的文件。这个我需要从果冻豆到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;
}
4

0 回答 0