简体   繁体   English

Android目录创建在内部存储上失败

[英]Android Directory creation failing on internal storage

I want to create a new folder/directory called lists but it just keeps on failing, if anyone had any suggestions that would be greatly appreciated. 我想创建一个名为列表的新文件夹/目录,但是如果有人有任何建议,将不胜枚举,它将继续失败。 Thank you. 谢谢。

File file = new File(this.getFilesDir() +File.separator +"Lists");

if(file.mkdirs()){
    System.out.println("Directory success");
}else{
    System.out.println("Directory failed!");
}

System.out.println(file.getAbsolutePath());
if(file.isDirectory()){
    System.out.println("File is a directory");
    File lists[] = file.listFiles();
    String names[] = new String[lists.length];
    if(lists.length >0){
        for(int i = 0; i < lists.length; i ++){
            names[i] = lists[i].getName();
        }
    }else{
        names[0] = "Create New List";
    }
}else{
    System.out.println("File is not a directory");
}

My first answer is completely incorrect (my apologies for that)! 我的第一个答案是完全错误的(对此我深表歉意)! I believe this one is correct. 我相信这是正确的。

I think problem lies with the following lines: 我认为问题在于以下几行:

File lists[] = file.listFiles();
String names[] = new String[lists.length];
if(lists.length > 0){
    for(int i = 0; i < lists.length; i ++){
        names[i] = lists[i].getName();
    }
}else{
    names[0] = "Create New List";
}

You had just created the folder so there will be no files in it so lists[] won't have any size, but in your else statement you are adding an item to the names array even though it doesn't have a size. 您刚刚创建了该文件夹,所以其中没有文件,因此lists[]不会有任何大小,但是在else语句中,即使没有大小,也要向names数组中添加一个项目。 Removing names[0] = "Create New List"; 删除names[0] = "Create New List"; from the else statement should work. 从else语句应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM