0

为什么这段代码不起作用: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

4

1 回答 1

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());
        }
于 2012-06-26T15:43:09.350 回答