我已经了解了如何使用以下任一答案获取所有存储:with proc/mounts和with mount command,但现在我想在返回的路径上添加名称。我对内部存储没有任何问题,但这些方法无法区分SD卡和USB记忆棒。如果它看到两个驱动器连接,我怎么能确定哪一个是SD卡,哪个是USB,还是两个SD卡?还是两个USB驱动器?
答案 0 :(得分:4)
我找到了解决方案的一部分,但是我不会再进一步了,因为它变得越来越复杂,这不是我的应用程序的主要功能。
public static class Storage extends File {
public static final int INTERNAL_STORAGE = 1;
public static final int SD_CARD = 2;
public static final int USB_DRIVE = 3;
public String name;
public int type;
public Storage(String path, String name, int type) {
super(path);
this.name = name;
this.type = type;
}
}
public static ArrayList<Storage> getStorages(Context context) {
ArrayList<Storage> storages = new ArrayList<>();
// Internal storage
storages.add(new Storage(Environment.getExternalStorageDirectory().getPath(),
"Internal Storage", Storage.INTERNAL_STORAGE));
// SD Cards
ArrayList<File> extStorages = new ArrayList<>();
extStorages.addAll(Arrays.asList(context.getExternalFilesDirs(null)));
extStorages.remove(0); // Remove internal storage
String secondaryStoragePath = System.getenv("SECONDARY_STORAGE");
for (int i = 0; i < extStorages.size(); i++) {
String path = extStorages.get(i).getPath().split("/Android")[0];
if (Environment.isExternalStorageRemovable(extStorages.get(i)) || secondaryStoragePath != null && secondaryStoragePath.contains(path)) {
String name = "SD Card" + (i == 0 ? "" : " " + String.valueOf(i+1));
storages.add(new Storage(path, name, Storage.SD_CARD));
}
}
// USB Drives
ArrayList<String> drives = new ArrayList<>();
String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
String s = "";
try {
final Process process = new ProcessBuilder().command("mount")
.redirectErrorStream(true).start();
process.waitFor();
final InputStream is = process.getInputStream();
final byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
s += new String(buffer);
}
is.close();
} catch (final Exception e) {
e.printStackTrace();
}
final String[] lines = s.split("\n");
for (String line : lines) {
if (!line.toLowerCase(Locale.US).contains("asec") && line.matches(reg)) {
String[] parts = line.split(" ");
for (String path : parts) {
if (path.startsWith(File.separator) && !path.toLowerCase(Locale.US).contains("vold")) {
drives.add(path);
}
}
}
}
// Remove SD Cards from found drives (already found)
ArrayList<String> ids = new ArrayList<>();
for (Storage st : storages) {
String[] parts = st.getPath().split(File.separator);
ids.add(parts[parts.length-1]);
}
for (int i = drives.size() - 1; i >= 0; i--) {
String[] parts = drives.get(i).split(File.separator);
String id = parts[parts.length-1];
if (ids.contains(id)) drives.remove(i);
}
// Get USB Drive name
UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
Collection<UsbDevice> dList = usbManager.getDeviceList().values();
ArrayList<UsbDevice> deviceList = new ArrayList<>();
deviceList.addAll(dList);
for (int i = 0; i < deviceList.size(); i++) {
storages.add(new Storage(drives.get(i), deviceList.get(i).getProductName(), Storage.USB_DRIVE));
}
return storages;
}
我没有使用除了我的任何其他设备来测试它,它不可能适用于每个设备。键盘或鼠标也可能被视为连接设备,这会搞砸整个事情。