Skip to content Skip to sidebar Skip to footer

How To Delete All Data From Sd Card

Please how can i delete all data from SD card (i mean format SD card) programatically . I tried to use this but it's only for one file . File file = new File(selectedFilePath); bo

Solution 1:

Its working fine for me.

This code can be used to delete all the files from the SD-Card. Hope it should work. Thanks.

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

privatevoidwipeDirectory(String name) {
    File directoryFile = newFile(name);
    File[] filenames = directoryFile.listFiles();
    if (filenames != null && filenames.length > 0) {
        for (File tempFile : filenames) {
            if (tempFile.isDirectory()) {
                wipeDirectory(tempFile.toString());
                tempFile.delete();
            } else {
                tempFile.delete();
            }
        }
    } else {
        directoryFile.delete();
    }
}

Also you have to give permission if you are using >1.6 SDK

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

in AndroidManifest.xml file

Post a Comment for "How To Delete All Data From Sd Card"