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

时间:2014-12-01 11:16:19

标签: android android-sdcard

如何检查SD卡的文件系统类型?

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

提前致谢。

2 个答案:

答案 0 :(得分:5)

一种解决方案是运行 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文件系统类型:

答案 1 :(得分:1)

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

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