0

我想格式化 SD 卡,我正在使用以下代码。但是SD卡不能挂载。

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

有人可以告诉我我做错了什么吗?谢谢

4

1 回答 1

0

尝试这个

 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();
    }
}
于 2012-10-31T09:45:23.963 回答