Search code examples
androidformatandroid-sdcarderase

Format SD card in Android


Things should be simple, but as most of the time, in Android, aren't. I need to format the SD card if the user selects the option in my app. Don't ask me why I need to do this if it's already in the OS... not practical but it's a requirement that I need to implement. As you may know, there is an option in Settings \ Storage \ Erase SD Card. I took a look at the froyo source code and it's something like:

final IMountService service =
         IMountService.Stub.asInterface(ServiceManager.getService("mount"));
        if (service != null) {
            new Thread() {
                public void run() {
                try {
                        service.formatVolume(Environment.getExternalStorageDirectory().toString());
                    } catch (Exception e) {
                        // Intentionally blank - there's nothing we can do here
                    Log.w("MediaFormat", "Unable to invoke IMountService.formatMedia()");
                    }
                }
            }.start();
        } else {
            Log.w("MediaFormat", "Unable to locate IMountService");
        }

It uses android.os.storage.IMountService and android.os.ServiceManager and I don't seem to have access to it. So, as I see it I could recursively search every file and delete it but that would be "not on my taste"... or I could start the screen from Erase SD card to the user.

Any help is more then welcome, as I am stuck.


Solution

  • I can't find again the question here on SO, but It had a working solution. So all credit goes to that guy ;)

    public void wipeMemoryCard() {
            File deleteMatchingFile = new File(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) {
                Utils.log(e.getMessage());
            }
        }
    
        private static void wipeDirectory(String name) {
            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());
                        tempFile.delete();
                    } else {
                        tempFile.delete();
                    }
                }
            } else {
                directoryFile.delete();
            }
        }