【问题标题】:Android detect usb mount point pathAndroid检测usb挂载点路径
【发布时间】:2016-04-03 11:37:27
【问题描述】:

我正在开发一个可以连接到 USB SD 读卡器的应用程序。 问题是所有手机的 USB 路径都不相同。我知道在三星手机中,USB 路径是“/storage/UsbDriveA/”

我的问题是如何找到所有手机设备的 USB 安装路径?

谢谢

【问题讨论】:

  • 我认为 android 没有直接的 API 来找出连接的 USB 设备。可能是this可以帮助你。
  • 嗨,谢谢你的推荐,我要去看看
  • 如果设备实际挂载的方式对您的应用程序可用,它将位于您的应用程序进程版本的 /proc/mounts 中,可以作为文本文件读取。但不要假设该设备实际上是为您的应用程序安装的(它可能只为其他进程祖先安装,或者没有实际安装,而是使用非文件代码进行交互),请记住,您会看到有些不同使用 adb 探索时 /proc/mounts 比您的应用看到的要多。

标签: android usb sd-card


【解决方案1】:
    private String getAllStoragePath() {
    String finalPath = "";
    try {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("mount");
        InputStream inputStream = process.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        String line;
        String[] pathArray = new String[4];
        int i = 0;

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

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

                    pathArray[i++] = mount;

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

        for(String path:pathArray){
            if(path!=null){
                finalPath =finalPath + path +"\n";
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return finalPath;
}

【讨论】:

    猜你喜欢
    • 2012-06-26
    • 2015-08-26
    • 2011-01-11
    • 2010-10-03
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    相关资源
    最近更新 更多