【问题标题】:How do I check the type of sdcard's filesystem?如何检查 sdcard 文件系统的类型?
【发布时间】:2014-12-01 11:16:19
【问题描述】:

如何查看sdcard的文件系统类型?

例如,在 Windows 中我们可以看到 NTFS、FAT32、exFAT 等。

提前致谢。

【问题讨论】:

    标签: android android-sdcard


    【解决方案1】:

    一种解决方案是运行 mount 命令,然后对结果进行一些字符串处理:

        try{
            Process mount = Runtime.getRuntime().exec("mount");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(mount.getInputStream()));
            mount.waitFor();
    
            String extPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            String line;
            while ((line = bufferedReader.readLine()) != null)
            {
                String[] split = line.split("\\s+");
                for(int i = 0; i < split.length - 1; i++)
                {
                    if(split[i].contentEquals(extPath) ||
                        split[i].contains("sdcard") ||
                        split[i].contains("_sd") ||
                        split[i].contains("extSd") ||
                        split[i].contains("_SD"))   // Add wildcards to match against here
                    {
                        String strMount = split[i];
                        String strFileSystem = split[i+1];
    
                        // Add to a list/array of mount points and file systems here...
                        Log.i("SDCard", "mount point: "+ strMount + " file system: " + strFileSystem);
                    }
                }
            }           
        }catch(IOException e){
            e.printStackTrace();            
        }catch(InterruptedException e){
            e.printStackTrace();            
        }
    

    在我的 G Pro 上运行它给了我以下结果:

    mount point: /mnt/media_rw/external_SD file system: vfat
    mount point: /storage/external_SD file system: fuse
    

    一些常见的Android文件系统类型:

    【讨论】:

    • 感谢您之前的回答,当我得到帮助时我会接受。我有以下结果:“挂载点:/mnt/sdcard/.android_secure 文件系统:tmpfs”。但是根据您的结果:“挂载点:/mnt/media_rw/external_SD 文件系统:vfat”和“挂载点:/storage/external_SD 文件系统:保险丝”。那么,这意味着什么?什么是 tmpfs、vfat 和 fuse?
    • 我已经为我很多朋友的手机 (JellyBean) 测试了你的代码。它仅适用于手机内存(1.4GB),不适用于他们的 SD 卡。如果您能再次帮助我,我会投票支持您的评论或投票支持您的回答。谢谢...
    • 我已经编辑了答案以包含更多要匹配的字符串。 SD卡路径因手机制造商而异。这个问题有更多信息:stackoverflow.com/questions/23123461/… 你也可以匹配从 Environment.getExternalStorageDirectory() 返回的结果。另外,检查您朋友手机上的路径并将其添加到列表中。
    • 再次感谢,非常有帮助。 :)
    • @UbuntuForums_Staff_Are_Trolls Java
    【解决方案2】:

    对于自定义路径,我们可以使用这个静态方法:

    public static String getFileSystem(File path){
        try{
            Process mount = Runtime.getRuntime().exec("mount");
            BufferedReader reader = new BufferedReader(new InputStreamReader(mount.getInputStream()));
            mount.waitFor();
    
            String line;
            while ((line = reader.readLine()) != null) {
                String[] split = line.split("\\s+");
                for (int i = 0; i < split.length - 1; i++){
                    if (!split[i].equals("/") && path.getAbsolutePath().startsWith(split[i]))
                        return split[i + 1];
                }
            }
            reader.close();
            mount.destroy();
        }catch(IOException | InterruptedException e){
            e.printStackTrace();
        }
        return null;
    }
    

    例如下面的代码:

    Log.i("---", "Filesystem = "+ getFileSystem(Environment.getExternalStorageDirectory()));
    

    将产生这个输出:

    I/---: Filesystem = vfat
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 2022-07-15
      • 2014-11-30
      相关资源
      最近更新 更多