1

在Android开发中,我们如何通过编码将整个SD卡擦除?我需要通过我的应用程序格式化 SD 卡的内容。

4

1 回答 1

2

我找到了答案,只需调用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) {
    }
}
于 2013-01-17T00:58:56.877 回答