Check sd-card availablity android

169 views Asked by At

In my application I want to check whether the SD-Card is present (asin mounted) or not. When I try to run the application I am getting "sd-card is mounted" even though its not there.

Code

public boolean isSDCardPresent() {

        return android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

    }

The above code always returns true even if the sd-card is not present.

4

There are 4 answers

0
sudo_rizwan On

The external (removable) Sd path varies from device to device, I also couldn't figure out a single way to check its availability, so I wrote a method, which iterates through all the different ext paths that the different manufacturers use, and then it finds the exact match. Returns true if the folder is found & the card is inserted in the phone.

Note: I used StreamSupport library inside the method, so you'll need to download the jar file and add it to libs folder of your project and that's it, it'll work!

   public static boolean isExternalCardAvailable(Context context) {
    List<String> listOfFoldersToSearch = Arrays.asList("/storage/", "/mnt/", "/removable/", "/data/");
    final List<String> listOf2DepthFolders = Arrays.asList("sdcard0", "media_rw", "removable");
    final List<String> listOfExtFolders = Arrays.asList("sdcard1", "extsdcard", "external_sd", "microsd", "emmc", "ext_sd", "sdext",
            "sdext1", "sdext2", "sdext3", "sdext4");
    final String[] thePath = {""};
    Optional<File> optional = StreamSupport.stream(listOfFoldersToSearch)
            .filter(new Predicate<String>() {
                @Override
                public boolean test(final String s) {
                    File folder = new File(s);
                    return folder.exists() && folder.isDirectory();
                }
            }) //I got the ones that exist and are directories
            .flatMap(new Function<String, Stream<File>>() {
                @Override
                public Stream<File> apply(final String s) {
                    try {
                        List<File> files = Arrays.asList(new File(s).listFiles());
                        return StreamSupport.stream(files);
                    } catch (NullPointerException e) {
                        return StreamSupport.stream(new ArrayList<File>());
                    }
                }
            }) //I got all sub-dirs of the main folders
            .flatMap(new Function<File, Stream<File>>() {
                @Override
                public Stream<File> apply(final File file1) {
                    if (listOf2DepthFolders.contains(file1.getName()
                            .toLowerCase())) {
                        try {
                            List<File> files = Arrays.asList(file1.listFiles());
                            return StreamSupport.stream(files);
                        } catch (NullPointerException e) {
                            return StreamSupport.stream(Collections.singletonList(file1));
                        }
                    } else
                        return StreamSupport.stream(Collections.singletonList(file1));
                }
            }) //Here I got all the 2 depth and 3 depth folders
            .filter(new Predicate<File>() {
                @Override
                public boolean test(final File o) {
                    return listOfExtFolders.contains(o.getName()
                            .toLowerCase());
                }
            })
            .findFirst();

    optional.ifPresent(new Consumer<File>() {
        @Override
        public void accept(final File file) {
            thePath[0] = file.getAbsolutePath();
        }
    });

    Log.e("Path", thePath[0]);

    try {
        ContextCompat.getExternalFilesDirs(context, null);
    } catch (Exception e) {
        Log.e("PathException", thePath[0]);
    }
    return !thePath[0].equals("") && new File(thePath[0]).listFiles()!=null;
}

P.S. I tested and verified it on a few HTC and Samsung devices.

5
Bidhan On

Try changing your method to this

public boolean isSDCardPresent() {

    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

}
2
Anand Singh On

Try below code:

public boolean isSDCardPresent() { 
    return android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
}
0
Sir Cancle A Lot On

I had a pretty similar problem yesterday:

I wanted to check, if there's a physical Sd-Card in my device.

the Problem was that

public boolean isSDCardPresent() {

return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);}

always returned true (even if the sd-Card was physically removed) , because of the emulated sd-card.

so this is my solution:

static boolean externalStoragecheck() {
    return !Environment.isExternalStorageEmulated();
}

Haven't found anything like this, so I wanted to share this with you.