如何确定文件是否在USB大容量存储设备上?

时间:2017-05-17 17:49:07

标签: java android android-studio usb-mass-storage

鉴于FileUri,我如何可靠地确定该文件是否位于连接到手机USB端口的USB大容量存储设备上?

我的测试手机,三星Galaxy S7 Edge,连接了USB大容量存储设备,内部(物理)微型SD卡和内部存储。当我查询Context.getExternalCacheDirs()时,我看到了所有这三个:

/storage/emulated/0/Android/data/...
/storage/A2B3-15F3/Android/data/...
/storage/3535-3038/Android/data/...

如何确定哪一个是USB大容量存储设备? (顺便说一下,这是A2B3-15F3。)

研究

我查看了this answerStorageHelper.resolveType()方法),但正如您所看到的,设备的挂载文件路径不包含字符串“usb”。

然后我尝试使用StorageManager服务。使用上述设备,我们可以使用:

获取StorageVolume`
StorageManager storageManager = context.getSystemService(StorageManager.class);
StorageVolume volume = storageManager.getStorageVolume(context.getExternalCacheDirs()[1]);

这是事情有点奇怪的地方。安卓25源代码中的StorageVolume API docs和我本地文件副本似乎对我没什么帮助,但是当我在Android Studio调试器中执行上述代码时,该对象包含mSubSystem将上述三个设备分别描述为“fuse”,“usb”和“sd”的字段。这是一个例子:

Screenshot of Android Studio debugger showing a StorageVolume for a USB mass storage device

为什么我在调试器中看到这些信息,而不是源代码?此字段是否特定于此特定手机的Android发行版?

2 个答案:

答案 0 :(得分:0)

Environment.getExternalStorageDirectory()将返回SD卡挂载点的路径。 USB存储仅支持极少数设备(支持 USB主机模式进行外部存储的设备)。这将安装在/mnt下的某个位置,但具体位置会有所不同。您需要使用Android NDK来查询和迭代已安装的设备以找到您之后的那个。在NDK和NDK下编写逻辑/mnt内的查询是一条可靠的路线。

如果您真的想尝试java方面,请尝试以下代码,如果它适合您:

public String getUSBStoragepath() {
    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("mount");
        InputStream input_stream = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(input_stream);
        String line;
        String[] paths_list = new String[10];
        int i = 0;
        int available = 0;

        BufferedReader buffer_reader = new BufferedReader(isr);
        while ((line = buffer_reader.readLine()) != null) {
            String mount = new String();
            if (line.contains("secure"))
                continue;
            if (line.contains("asec"))
                continue;

            if (line.contains("fat")) {// TF card
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    mount = mount.concat(columns[1] + "/requiredfiles");

                    paths_list[i] = mount;
                    i++;

                    // check directory is exist or not
                    File dir = new File(mount);
                    if (dir.exists() && dir.isDirectory()) {
                        // do something here

                        available = 1;
                        final_path = mount;
                        break;
                    } else {

                    }
                }
            }
        }
        if (available == 1) {

        } else if (available == 0) {
            final_path = paths_list[0];
        }

    } catch (Exception e) {

    }
    return final_path;
}

然后,您需要在最终路径中查询是否存在相关文件,以确定给定文件是否为USB格式。 < / p>

答案 1 :(得分:0)

您可以通过USBManager,连接的USB设备列表检查USB大容量存储是否已连接到手机,然后检查USB接口是否为USB_CLASS_MASS_STORAGE类。 然后询问用户是否断开USB大容量存储,保存SD卡路径并要求用户重新连接USB。其余的dirs将是USB路径。

相关问题