在某些设备中,它会返回可移动SD卡的实际路径,但在某些设备中它会返回null。
基本上,我想根据用户的内部存储设备或外部存储文件(可移动存储(micro 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;
}