Get all storages and devices with their names android

前提是你 提交于 2020-01-07 07:41:11

问题


I already found out how to get all storages with either of these answers: with proc/mounts and with mount command, but now I want to put a name on the paths returned. I have no problem with Internal Storage, however these methods can't distinguish between a SD card and a USB stick. If it sees two drives connected how can I be sure which one is the SD Card and which is the USB, or are they two SD Cards? Or two USB drives?


回答1:


I found part of the solution, however I won't get any farther because it's getting complicated and this is not the main feature of my app anyway.

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;
}
  • Find internal storage
  • Find all SD Cards
  • Find all external drives, then remove SD Cards from them, because they were already found. The point of this is to separate SD Cards from USB devices.

I didn't test this with any other device than mine, it won't likely work for every device. Also it is possible that keyboards or mouses counts as connected devices and that will screw up the whole thing.



来源:https://stackoverflow.com/questions/41719986/get-all-storages-and-devices-with-their-names-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!