如何获取otg连接线插入的u盘路径?

htmlchen 2014-01-15 11:01:27
用otg线接了一个u盘,想获取他们的路径. 我有两个安卓机,可是他们的路径都不一样啊...有一个是/mnt/usb_storage/, 还有一个是/mnt/sda1/ 这怎么获取啊,有统一的获取方法么, environment里面好像只有获取sdcard路径的接口....
...全文
334 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
儿大不由爷 2014-01-15
  • 打赏
  • 举报
回复
这个只能根据厂商实现来适配了,都是厂商自己定的,没标准。
niekangshibendan 2014-01-15
  • 打赏
  • 举报
回复
## Vold 2.0 Generic fstab ## - San Mehat (san@android.com) ## ####################### ## Regular device mount ## ## Format: dev_mount <label> <mount_point> <part> <sysfs_path1...> ## label - Label for the volume ## mount_point - Where the volume will be mounted ## part - Partition # (1 based), or 'auto' for first usable partition. ## <sysfs_path> - List of sysfs paths to source devices ###################### ## Example of a standard sdcard mount for the emulator / Dream # Mounts the first usable partition of the specified device #dev_mount sdcard /mnt/sdcard auto /devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1 #dev_mount flash /mnt/sdcard auto /devices/virtual/mtd/mtd9/mtdblock9 dev_mount flash /mnt/sdcard auto /devices/virtual/mtd/mtd9/mtdblock9 dev_mount sdcard /mnt/external_sd auto /devices/platform/rk29_sdmmc.0/mmc_host/mmc0 dev_mount udisk /mnt/usb_storage auto /devices/platform/usb20_host/usb /devices/platform/usb20_otg/usb ## Example of a dual card setup # dev_mount left_sdcard /sdcard1 auto /devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1 # dev_mount right_sdcard /sdcard2 auto /devices/platform/goldfish_mmc.1 /devices/platform/msm_sdcc.3/mmc_host/mmc1 ## Example of specifying a specific partition for mounts # dev_mount sdcard /sdcard 2 /devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1 这个是我的平板的vold.fstab内容,好吧我记错了,/mnt/sdcard代表的内置ROM /mnt/usb_storage是OTG连接的路径 /mnt/external_sd是外置SD卡
niekangshibendan 2014-01-15
  • 打赏
  • 举报
回复
public class Dev_MountInfo implements IDev { /* ####################### ## Regular device mount ## ## Format: dev_mount <label> <mount_point> <part> <sysfs_path1...> ## label - Label for the volume ## mount_point - Where the volume will be mounted ## part - Partition # (1 based), or 'auto' for first usable partition. ## <sysfs_path> - List of sysfs paths to source devices ###################### */ public final String HEAD = "dev_mount"; private final int DEV_INTERNAL = 0; private final int DEV_EXTERNAL = 1; private final int DEV_USB = 2;//这个可能没有,上面两个一般都会有 private ArrayList<String> cache = new ArrayList<String>(); private static Dev_MountInfo dev; private DevInfo info; private final File VOLD_FSTAB = new File( Environment.getRootDirectory().getAbsoluteFile() + File.separator + "etc" + File.separator + "vold.fstab"); public static Dev_MountInfo getInstance() { if (null == dev) dev = new Dev_MountInfo(); return dev; } private DevInfo getInfo(final int device) { try { initVoldFstabToCache(); } catch (IOException e) { e.printStackTrace(); } info = new DevInfo(); if(device < cache.size()){ String[] sinfo = cache.get(device).split(" |\t"); if(sinfo.length >= 5){ info.setLabel(sinfo[1]); info.setMount_point(sinfo[2]); info.setPart(sinfo[3]); info.setSysfs_path(sinfo[4]); } } return info; } /** * init the words into the cache array * @throws IOException */ private void initVoldFstabToCache() throws IOException { cache.clear(); BufferedReader br = new BufferedReader(new FileReader(VOLD_FSTAB)); String tmp = null; while ((tmp = br.readLine()) != null) { // the words startsWith "dev_mount" are the SD info if (tmp.startsWith(HEAD)) { cache.add(tmp); Log.v("xcl", tmp); } } br.close(); cache.trimToSize(); } public class DevInfo { private String label, mount_point, part; private String sysfs_path; public String getLabel() { return label; } private void setLabel(String label) { this.label = label; } public String getMount_point() { return mount_point; } private void setMount_point(String mount_point) { this.mount_point = mount_point; } public String getPart() { return part; } private void setPart(String part) { this.part = part; } public String getSysfs_path() { return sysfs_path; } private void setSysfs_path(String sysfs_path) { this.sysfs_path = sysfs_path; } } public DevInfo getInternalInfo() { return getInfo(DEV_INTERNAL); } public DevInfo getExternalInfo() { return getInfo(DEV_EXTERNAL); } public DevInfo getUSBInfo(){ return getInfo(DEV_USB); } } interface IDev { DevInfo getInternalInfo(); DevInfo getExternalInfo(); } 调用代码: //获得各盘符路径 Dev_MountInfo dev = Dev_MountInfo.getInstance(); DevInfo info = dev.getInternalInfo(); this.diskInternal = info.getMount_point(); //Environment.getExternalStorageDirectory() 这两个要是不一样咋办 info = dev.getExternalInfo(); this.diskExternal = info.getMount_point(); info = dev.getUSBInfo(); this.diskUSB = info.getMount_point();
参考:http://blog.csdn.net/xubright/article/details/9832607
niekangshibendan 2014-01-15
  • 打赏
  • 举报
回复
usb_storage一般是外置SD卡的路径,通过OTG连U盘几乎都是从usbhost0开始计数的,不过偶尔也有例外! 可以用记事本打开"system/etc/vold.fstab"这个文件,里面记录了外接设备的名称,识别方法自己百度或者调试!

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧