1

How to check the type of sdcard's filesystem?

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

Thanks in advance.

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87

2 Answers2

6

One solution is to run the mount command then do some string processing on the result:

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

Running this on my G Pro gave me the following results:

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

Some common Android file system types:

samgak
  • 23,944
  • 4
  • 60
  • 82
  • Thanks for your answer before and I will accept it when I was helped. I have the following results: "mount point: /mnt/sdcard/.android_secure file system: tmpfs". But on your result: "mount point: /mnt/media_rw/external_SD file system: vfat" and "mount point: /storage/external_SD file system: fuse". So, what does it mean? What are tmpfs, vfat and fuse? – Anggrayudi H Jan 01 '15 at 13:46
  • I have tested your code for many of my friends' phone (JellyBean). It works on phone memory only (1.4GB), not on their sdcard. I would vote your comment up or vote your answer up if you could help me again. Thanks... – Anggrayudi H Jan 10 '15 at 02:36
  • 1
    I have edited the answer to include more strings to match against. The SD card path is different for different phone manufacturers. This question has more information: http://stackoverflow.com/questions/23123461/external-sdcard-file-path-for-android You can also match against the result returned from Environment.getExternalStorageDirectory(). Also, check what the path is on your friend's phone and add it to the list. – samgak Jan 10 '15 at 03:40
  • 1
    @UbuntuForums_Staff_Are_Trolls Java – samgak May 29 '20 at 21:40
2

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
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87