简体   繁体   中英

formatting SD CARD in Android

I want to format SD card, I am using the follow code. But the SD card can not mount.

public boolean formatSD() {
        boolean bool = false;
        IMountService mountService = null;
        try{
            IBinder ibi = ServiceManager.getService("mount");
            if(ibi!=null) {
                mountService = IMountService.Stub.asInterface(ibi);
                String extern_sd = Environment.getFlashStroageDirectory().getAbsolutePath();
                mountService.unmountVolume(extern_sd, true);

                Thread.sleep(4000);
                int result_format = mountService.formatVolume(extern_sd);       
                System.out.println("result_format: "+result_format);          
                if(result_format==0){
                    Thread.sleep(4000);

                    int result_mount = mountService.mountVolume(extern_sd);
                    System.out.println("result_mount:  "+ result_mount);
                }
                bool = true;
            }
        }catch(Exception e){
            System.out.println("Exception format: "+e.getMessage());
        }
        return bool;
    }

Can somebody please tell me what i did wrong? Thanks

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM