2 clcurie 11 clcurie_11 于 2013.05.30 14:37 提问

不能删除 sdcard 中的文件

如何删除sdcard中的文件?

public void onClick(DialogInterface dialog, int which) {
                      File dir = new File (Environment.getExternalStorageDirectory() + "/MyApp/MyFolder");
                       if (dir.isDirectory()) 
                       {
                           File file = new File(card.imagePath);
                           file.delete();
                       } 

2个回答

Jave_
Jave_   2013.05.30 15:20
已采纳
    public static void delDir(File file) {
    boolean bool = false;
    if (file.isFile()) {
        bool = file.delete();
        System.out.println("Delete file[" + file.getAbsolutePath() + "," + bool + "]");
    } else {
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                if (child.isFile()) {
                    bool = child.delete();
                    System.out.println("Delete file[" + child.getAbsolutePath() + "," + bool + "]");
                } else {
                    delDir(child);
                }
            }
        }
        bool = file.delete();
        System.out.println("Delete folder[" + file.getAbsolutePath() + "," + bool + "]");
    }
}

public static void delDir(String path) {
    File file = new File(path);
    delDir(file);
}
wurenbi45
wurenbi45   2013.05.30 15:35

manifest.xml中添加sdcard操作权限了没

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!