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条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置