How can I programmatically format all data on an SD Card?

試著忘記壹切 提交于 2019-11-27 17:01:47

问题


How can I programmatically delete data or format the entire SD Card?


回答1:


code to wipe SDCARD

public void wipingSdcard() {
        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) {
            e.printStackTrace();
        }
    }

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



回答2:


Try reading up on this one How to delete a file from SD card?




回答3:


By using the File class you should be able to list all the files on the SDCard and to delete each of them one by one. You'll make a recursive function to delete directories. It is however not such a good idea and the OS might prevent you from deleting some of the files/folders used by the system or another user.



来源:https://stackoverflow.com/questions/4749891/how-can-i-programmatically-format-all-data-on-an-sd-card

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!