1

i have written following code to create directory in android but it is not working.

   File root = Environment.getExternalStorageDirectory();
   File directory = new File(root.getAbsolutePath()+"/MYFOLDER");
   file.mkdirs();
  • have you added permissions code ? – Pavneet_Singh Jan 02 '17 at 13:31
  • Please explain, **in detail**, how you have determined that "it is not working". – CommonsWare Jan 02 '17 at 13:40
  • @PavneetSingh yes i have added permission code – Saujan Baniya Jan 02 '17 at 14:57
  • @CommonsWare what i meant to say was, i write those above codes to create a directory in my external storage but it doesn't create a directory. When i view the directory in above codes it show the path as "storage/emulated/0" . and i have android of version marshmallow. – Saujan Baniya Jan 02 '17 at 14:59
  • "When i view the directory in above codes it show the path as "storage/emulated/0"" -- `/storage/emulated/0/` is a typical base path for the root of external storage for the default user. Please explain, **in detail**, how you have determined that "it doesn't create a directory". – CommonsWare Jan 02 '17 at 15:06
  • @CommonsWare I want to create a directory in external storage. Can you help me to do so? – Saujan Baniya Jan 02 '17 at 15:18
  • Your code already does that, assuming that you have the `WRITE_EXTERNAL_STORAGE` permission and have requested that permission at runtime as well. I recommend using `new File(root.getAbsolutePath(), "MYFOLDER")` instead of what you have. You may not be able to *see* that directory, depending on what you are using to look for it. That is why I keep asking you to explain how you are looking for it. – CommonsWare Jan 02 '17 at 15:21
  • @CommonsWare I tried the way you said but also it is not working.what i was looking was to create a directory in external storage and create a file inside that directory. – Saujan Baniya Jan 02 '17 at 15:32
  • @CommonsWare i am using android marshmallow version – Saujan Baniya Jan 02 '17 at 15:35
  • Please explain, **in detail**, how you have determined that "it is not working". **This is now the fourth time I have asked this**. Are you using `adb shell ls /storage/emulated/0`? Are you using the Android Device Monitor? Are you using an on-device file manager? Are you examining external storage using your development machine's file manager (e.g., where you have mounted external storage as a volume using a USB cable)? Are you using something else? – CommonsWare Jan 02 '17 at 15:40
  • @CommonsWare Sorry, since i am new to android, I didnt undersand your question. i am using on device file manager – Saujan Baniya Jan 02 '17 at 15:53
  • 1
    Your file manager may not be looking at the filesystem itself, but instead at what is indexed by the `MediaStore`. Empty directories may not show up there. Use developer tools, like `adb shell ls /storage/emulated/0` or the Android Device Monitor, to see if the directory exists. Or, copy a file out to that directory, then [get that file indexed by the `MediaStore`](http://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl), and see if that file shows up in your file manager. – CommonsWare Jan 02 '17 at 16:02

1 Answers1

0

This is how you can create a directory in Android:

  File root = Environment.getExternalStorageDirectory();
  File directory = new File(root.getAbsolutePath()+"/MYFOLDER");
   If (!directory.exists){
      directory.mkdirs();
       }

Make sure you've permission granted to write external storage