lgxing 2012-11-15 02:03 采纳率: 100%
浏览 13353
已采纳

各位android的高手,请问怎么得到,判断一台机器上usb盘的目录是什么

各位android的高手,请问怎么得到,判断一台机器上usb盘的目录是什么
或者怎么判断一个路径是不是usb盘的

一个实例如下(我现在的一个android设备现状):
1,有内部存储器sdcard,具体路径:/mnt/sdcard
2,有tf卡,具体路径:/mnt/ext_sd
3,有u盘,具体路径:/mnt/usbhost1
我可以得到/mnt/usbhost1,并且确定其是u盘的路径

但是其他设备不一定会和我这个设备相同,
比如有的设备u盘路径可能是/mnt/sda1,tf卡设备路径可能是/mnt/tfcard

我想知道有没有方法可以获取到/mnt/sda1,并且确定其是u盘的路径而不是tf卡路径

我现在是通过以下方法判断的

String devicePath = intent.getDataString().toLowerCase().substring(7);
if (devicePath.contains("usb"))
{
   //这个是u盘
}

但是这并不准确,如上面的/mnt/sda1

  • 写回答

4条回答 默认 最新

  • Jobernowl 2012-11-15 03:37
    关注

    Environment.getExternalStoragePublicDirectory()
    Context.getExternalFilesDir()
    public static String getExtSDCard(){
    File[] files=new File("/mnt").listFiles();
    String sdcard=Environment.getExternalStorageDirectory().getAbsolutePath().toLowerCase();
    String file;
    for (int i = 0; i < files.length; i++) {
    file=files[i].getAbsolutePath().toLowerCase();
    if(!file.equals(sdcard)&&(file.contains("ext")||file.contains("sdcard"))){
    return file;
    }
    }
    return null;
    }

    改进一下
    你也可以遍历mnt路径下面的所有设备 使用

    StatFs statFs=new StatFs(path);
    

    来读取大小信息 大的自然就是存储卡了 然后再根据名字来排除内部sdcard即可获得外部存储卡路径
    public static String getExtSDCard(){
    File[] files=new File("/mnt").listFiles();
    String sdcard=Environment.getExternalStorageDirectory().getAbsolutePath();
    String file;
    for (int i = 0; i < files.length; i++) {
    file=files[i].getAbsolutePath();
    if(!file.equalsIgnoreCase(sdcard)&&file.contains("ext")){
    return file;
    }

        }
        return null;
    }
    

    你可以这么做 根据经验所有sdcard都在mnt下面 首先获取默认sdcard的路径 然后遍历mnt下面的所有设备 如果出了默认sdcard之外还有别的设备名字包含"ext"字符的 则返回该设备名字(极大可能是扩展sdcard)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料