1

In Android Development, how can we wipe out the whole SD card through coding? I need to format the SD card contents through my application.

Cœur
  • 37,241
  • 25
  • 195
  • 267
abhi
  • 503
  • 6
  • 24
  • 2
    Please search through SO and Google before posting. http://stackoverflow.com/questions/7405173/format-sd-card-in-android – Matt Clark Dec 06 '12 at 05:13
  • My friends, I searched a lot in Google and SO, but couldn't got any relevant answer. Thanks Matt Clark, I will have a look at the link you provided. Thanks!! – abhi Dec 06 '12 at 22:22

1 Answers1

2

I found the answer, just call wipeMemoryCard();

public void wipeMemoryCard() {
    try {
        File deleteMatchingFile = new File(Environment.getExternalStorageDirectory().toString());
        File[] filenames = deleteMatchingFile.listFiles();
        if (filenames != null && filenames.length > 0) {
            for (File tempFile : filenames) {
                    if (tempFile.isDirectory()) {
                        wipeDirectory(tempFile.toString());
                    } else {
                        tempFile.delete()
                    }
            }
        }
    } catch (Exception e) {
    }
}

private void wipeDirectory(String name) {
    try {
        File directoryFile = new File(name);
        File[] filenames = directoryFile.listFiles();
        if (filenames != null && filenames.length > 0) {
            for (File tempFile : filenames) {
                    if (tempFile.isDirectory()) {
                        wipeDirectory(tempFile.toString());
                    } else {
                        tempFile.delete()
                    }
            }
        }
    } catch (Exception e) {
    }
}
abhi
  • 503
  • 6
  • 24