从SD卡中删除文件

时间:2012-06-26 00:28:52

标签: android

为什么这段代码不起作用:S

public void delete(String name) {
    File file = new File(
            activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
            name);

    if (file.exists()) {
        String[] files = file.list();
        for (int i = 0; i < files.length; i++) {
            boolean deleted;
            if (new File(files[i]).isDirectory())
                delete(new File(files[i]).getName());
            else {
                deleted = new File(files[i]).delete();
                Log.d(new File(files[i]).getName(),
                        Boolean.toString(deleted));
            }
        }
    }
}

我正在使用许可

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

当我检查删除的值时,我发现它是假的:S

1 个答案:

答案 0 :(得分:0)

试试这个

public static boolean deleteDirectory(String name) {
            // TODO Auto-generated method stub
File path= new File(
            activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
            name);

            if( path.exists() ) {
                File[] files = path.listFiles();
                for(int i=0; i<files.length; i++) {
                    if(files[i].isDirectory()) {
                        deleteDirectory(files[i]);
                    }
                    else {
                        files[i].delete();
                    }
                }
            }
            return(path.delete());
        }