5

I have a button in my app called "Reset" which deletes an entire folder (user folder). After that, I am trying to create again the same folder and the first time I try it, it allows me to create the folder, but the second time I try to Reset and re-create the user folder, the app crashes because the mkdir() did not create the folder and I attempted to create a database on that folder. But, the weird thing is that, after crashing, the folder has been created.

I have the permission:

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

I have tried both methods:

f.mkdir();
f.mkdirs();

What could I make wrong? Any idea?

Mario Velasco
  • 3,336
  • 3
  • 33
  • 50
  • I have seen a lot of questions about this, but none of them have helped me. – Mario Velasco Sep 25 '14 at 10:24
  • 1
    Instead of **deleting and recreating the same folder**, why don't you just **empty** it? It's a smarter (and also optimized) solution. – Phantômaxx Sep 25 '14 at 10:25
  • I have thought about it, but what I want is totally delete all the user folder, I don't what any useless folder, even if it is empty. I think that this is a "dirty" solution. Although I will do it if I cannot fix it. – Mario Velasco Sep 25 '14 at 10:37
  • `I don't what any useless folder, even if it is empty` This is **nonsense**. If you recreate **the same** "useless folder" (and it's **empty**, when you create it), it's **much faster** and **safer** to make it empty rather than **erase it and recreate it**. – Phantômaxx Sep 25 '14 at 10:43
  • Returns true if the directory was created, false on failure or if the directory already existed. as per http://developer.android.com/reference/java/io/File.html#mkdir() Are you sure your folder is deleted for the second time ? – Vny Kumar Sep 25 '14 at 10:49
  • @FrankN.Stein The user folder I want to erase contains a lot of information, and I want to delete every kind of track of the user (even its folder) when the user desires to delete its profile. And what if he creates again its profile? The folder will be created again. Obviously, this is not very frequent, but I have to deal with it. – Mario Velasco Sep 25 '14 at 11:26
  • @VnyKumar Yes, I am totally sure. – Mario Velasco Sep 25 '14 at 11:26

2 Answers2

4

I also faced the same problem. But, finally, I think I found a solution (may be workaround).

When you delete the directory, rename it and delete. Then, usually, create the directory using File.mkdirs(). This should work fine. I tested in my case. It works!!!

public static final void renameAndDelete(File fileOrDirectory) {
    File newFile = new File(fileOrDirectory.getParent() + File.separator
                + "_" + fileOrDirectory.getName());
    fileOrDirectory.renameTo(newFile);
    delete(newFile);
}
public static final void delete(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
        for (File child : fileOrDirectory.listFiles())
            delete(child);

    fileOrDirectory.delete();
}
gopalanrc
  • 254
  • 3
  • 11
0

I think it is because you are calling a method to create the folder, Before folder gets created you are trying to create a DB into that folder!

Possible Solution

Try creating the database after the folder is created successfully. And while creating a database once again check if that folder/path exists.

Rohit
  • 647
  • 14
  • 30
  • I have tried to create the folder before, but like I said, mkdir does not work and returns false. – Mario Velasco Sep 25 '14 at 10:33
  • mkdir and mkdirs return false if the directory already exists, so that might be one reason for the failure. If you are using Java 7, you can use the Files class. It throws an IOException on error with pretty good desriptions Files.createDirectory(file.toPath()); – Rohit Sep 26 '14 at 09:59
  • Can I use Java 7 on Android? I don't think so. – Mario Velasco Sep 26 '14 at 12:44