【问题标题】:Get all storages and devices with their names android获取所有名称为 android 的存储和设备
【发布时间】:2017-01-18 12:54:42
【问题描述】:

我已经知道如何使用以下任一答案获取所有存储:with proc/mountswith mount command,但现在我想在返回的路径上命名。我对内部存储没有任何问题,但是这些方法无法区分 SD 卡和 USB 记忆棒。如果它看到连接了两个驱动器,我如何确定哪个是 SD 卡,哪个是 USB,或者它们是两个 SD 卡?还是两个 U 盘?

【问题讨论】:

    标签: android usb android-sdcard mount


    【解决方案1】:

    我找到了部分解决方案,但我不会再进一步​​,因为它变得越来越复杂,而且这不是我的应用程序的主要功能。

    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;
    }
    
    • 查找内部存储
    • 查找所有 SD 卡
    • 找到所有外部驱动器,然后从中取出 SD 卡,因为它们已经被找到。这样做的目的是将 SD 卡与 USB 设备分开。

    我没有用我以外的任何其他设备对此进行测试,它不可能适用于所有设备。此外,键盘或鼠标也可能算作连接设备,这会搞砸整个事情。

    【讨论】:

    • 很好的解决方案。
    • 谨慎使用此代码!最后一个 for 循环非常关键。代码对硬件做了很多假设。例如。它不适用于我的开发板上,其中 Wifi 适配器和触摸传感器也使用 USB 连接。
    • @OneWorld 是的,老实说,根本不使用它。一个更好的起点是看看开源文件管理器是如何做到的,比如Amaze File ManagerSimple File Manager
    猜你喜欢
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 2021-04-20
    相关资源
    最近更新 更多