How do I check the type of sdcard's filesystem?

前端 未结 2 1901
逝去的感伤
逝去的感伤 2020-12-20 06:46

How to check the type of sdcard\'s filesystem?

For example, in Windows we can see NTFS, FAT32, exFAT, etc.

Thanks in advance.

2条回答
  •  星月不相逢
    2020-12-20 07:33

    For custom path, we can use this static method:

    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;
    }
    

    For example, below code:

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

    will produce this output:

    I/---: Filesystem = vfat
    

提交回复
热议问题