0

I am using a Samsung NEXUS with Android 4.2.1. Today I deleted a folder that was created by an app that I developed. The app has been working fine until this for months. I manually deleted the folder (through ES3 file explorer app) for testing something. This did not give any error messages from Android and I thought it worked like any delete. However, I am not able to create THIS folder manually or through code anymore. Any other folder can be created and deleted either way.

File f = new File(Environment.getExternalStorageDirectory(), "myfolder");

System.out.println(f.mkdirs());
System.out.println(f.exists());

Both prints false.

Through the adb shell tried the following.

mkdir myfolder
mkdir failed for myfolder, File exists

However ls does not list myfolder

rmdir myfolder
rmdir failed for myfolder, No such file or directory

Any idea why this is happening or the way out would be highly appreciated.

Thanks in Advance.

Ashok
  • 435
  • 1
  • 4
  • 16
  • Have you got the right permissions in your manifest about accessing and writing on the SDCARD – RvdK Jul 18 '14 at 07:14
  • @RvdK Thanks for the response. Yes the app has got all the required permissions. Infact the problem here is not the app. I can get the app running by using a different name for the folder and it works just fine. The issue here is with the already created folder `myfolder` which I deleted and is now not visible anywhere in the sdcard (internal storage). However neither adb nor the file explorer allows a folder to be created with that name. I need to create the same folder in my tablet and avoid any changes in the source code. – Ashok Jul 18 '14 at 07:29

2 Answers2

0
To create a directory you can use the following code:

File dir = new File("path/to/your/directory");
try{
  if(dir.mkdir()) {
     System.out.println("Directory created");
  } else {
     System.out.println("Directory is not created");
  }
}catch(Exception e){
  e.printStackTrace();
}

To delete an empty directory, you can use this code:
boolean success = (new File("your/directory/name")).delete();
if (!success) {
   System.out.println("Deletion failed!");
}

To delete a non-empty directory, you can use this code:

public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    return dir.delete();
}


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Original Answer here http://stackoverflow.com/a/5693077/2931489

Try this answer

if (!sdcarddir.exists())
   sdcarddir.mkdir();

Check in all folders by doing this...

String[] myDirectories = {"","",""......}; // your list of directories

for (String directory : myDirectories ) {
  File file = new File(directory);
   if(file.exists() && file.isDirectory())
      // Do something you have found your directory

}

Or try this..

File f = new File(Environment.getExternalStorageDirectory() + "/somedir");
if(f.isDirectory()) {
   ....
}

else .... Make directory
Harsh Patel
  • 1,056
  • 2
  • 10
  • 20
  • 1
    Thanks for the suggested code. This is already working fine in my app. Please see my response to the comment above – Ashok Jul 18 '14 at 07:32
  • have u tested on other device ? – Harsh Patel Jul 18 '14 at 07:44
  • 1
    Please next time you copy someone elses answer, also provide a link: http://stackoverflow.com/a/5693077/47351 – RvdK Jul 18 '14 at 07:57
  • @BabulPatel yes, it runs fine on any other device. All I need is the folder created with the same name as before. This is not happening either manually or through the app. – Ashok Jul 18 '14 at 08:54
  • What error occur if you create folder manually. Check for that is your folder hidden or something ? Before any folder if you insert .(DOT) then android consider it as hidden folder... so check that – Harsh Patel Jul 18 '14 at 08:59
  • The output when manually creating (as included in the question) is that it says "File exists". In `adb shell`, `ls -la` does list the hidden folders but `myfolder` is not listed. So I understand it is not hidden. – Ashok Jul 18 '14 at 09:17
0

Restarted the tablet and the issue disappeared. Must have been a platform issue!

Ashok
  • 435
  • 1
  • 4
  • 16