【问题标题】:How to mount USB Path in android?如何在android中挂载USB路径?
【发布时间】:2012-06-26 12:23:51
【问题描述】:

我正在使用 android tab Ice Cream Sandwich 版本 4.0.3 来运行我的应用程序。我正在连接外部 USB 设备和 android Tab。如何以编程方式挂载外部 USB 设备的路径。因为我需要从 USB 设备浏览文件到我的 android 选项卡。

那么如何在android中挂载USB路径?

【问题讨论】:

  • 这取决于你的设备制造商,因为android只有Environment.getExternalStorageDirectory()概念。
  • 谢谢。我同意这取决于设备。但我需要解决方案。

标签: android usb mount


【解决方案1】:

already given answer check Android detect usb mount point path

  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;
}

【讨论】:

  • 感谢代码,我将其用作后备,我首先检查文件“proc/mounts”是否存在并读取它,如果不存在,则我通过此脚本读取它。我们不应该依赖“fat”或“vfat”新设备有时有“fuse”作为分区类型,它的大脚本检测分区,但它们以“/dev/”开头,可能是vfat或fuse,你可能有重复指向同一分区的路径,请注意这一点。
【解决方案2】:

我同意user370305

您可以查看存储设置。挂载路径似乎在那里(例如,/mnt/usbdisk_1.0/)。此外,您也许可以只查看 /mnt 并查看列出的内容;我相信这就是各种文件管理器应用程序所做的。 USB 驱动器似乎有许多挂载点;尚未挂载的会显示为空,而已挂载的则可让您浏览它们(使用 Astro 等文件浏览器应用)。

通过this link

【讨论】:

  • 这仅适用于供应商已通过非标准功能扩展 Android 以安装 USB 存储卷的设备。 Stock Android 在操作系统级别不支持 USB 驱动器。如果库存设备具有主机模式,则可以使用在应用程序本身中实现的大容量存储和文件系统代码在低级别与驱动器通信,但您只能靠自己,没有操作系统的文件系统功能的帮助。
猜你喜欢
  • 2016-04-03
  • 1970-01-01
  • 2017-06-01
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
相关资源
最近更新 更多