Android-以编程方式擦除SD卡

时间:2012-12-06 05:10:29

标签: android android-sdcard

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

1 个答案:

答案 0 :(得分: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) {
    }
}