Android 常见的外部拓展存储主要类型有SD卡和U盘设备.
Android低版本可以直接根据路径进行判断设备类型,具体方法如下:
一:获取Android所有存储设备路径,并将对应路径存入map中.
public static final String INTERNAL_SD = "INTERNAL_SD";
public static final String EXTERNAL_SD = "EXTERNAL_SD";
public static final String EXTERNAL_USB = "EXTERNAL_USB";
// 获取机器所有的存储设备地址
public static Map<String, File> getAllStorageLocations() {
Map<String, File> map = new HashMap<String, File>(10);
// 获取内部存储路径,存入INTERNAL_SD中
map.put(INTERNAL_SD, Environment.getExternalStorageDirectory());
Scanner scanner = null;
mUsbAvailCnt=0;
try {
File mountFile = new File("/proc/mounts");
if (mountFile.exists()) {
scanner = new Scanner(new BufferedReader(new FileReader("/proc/mounts")));//new Scanner(mountFile);
while (scanner.hasNext()) {
String line = scanner.nextLine();
// 获取SD卡对应路径,存入EXTERNAL_SD中
if (line.startsWith("/dev/block/vold/public:179")) {
String[] lineElements = line.split(" ");
String element = "/storage/" + lineElements[1].substring(lineElements[1].lastIndexOf("/"));
Log.d(TAG, "EXTERNAL_SD: " + element);
File ff = new File(element);
if (ff.exists() && ff.isDirectory()) {
Log.d(TAG, "EXTERNAL_SD +=" + ff.getPath());
map.put(EXTERNAL_SD, ff);
}
// 获取Upan对应路径,存入EXTERNAL_USB中
} else if (line.startsWith("/dev/block/vold/public:8")) {
String[] lineElements = line.split(" ");
String element = "/storage/" + lineElements[1].substring(lineElements[1].lastIndexOf("/"));
File ff = new File(element);
Log.d(TAG, "EXTERNAL_USB: " + element);
Log.d(TAG, "EXTERNAL_USB: " + line);
if (ff.exists() && ff.isDirectory()) {
Log.d(TAG, "EXTERNAL_USB +=" + ff.getPath());
mUsbAvailCnt++;
String key = EXTERNAL_USB + String.valueOf(mUsbAvailCnt);
map.put(key, ff);
}
}
}
}
} catch (Exception e) {
Log.d(TAG, "Exception: " + e);
} finally {
if (scanner != null) {
scanner.close();
}
}
return map;
}
public static String getSDCardStoragePath() {
Map<String, File> map = getAllStorageLocations();
if(map.get(EXTERNAL_SD) == null) {
return "null/";
}
else {
return map.get(EXTERNAL_SD).getPath() + "/";
}
}
public static String getUSBStoragePath() {
int index = getUSBStoragePathIndex();
String finalpath = "null";
if(mUsbPathArray != null && mUsbPathArray.length > 0) {
finalpath = mUsbPathArray[index];
}
Log.d(TAG, "finalpath: " + finalpath);
return finalpath + "/";
}