0

I am having trouble with creating a directory with android studio on my app. The problem is that my code is simply not creating a directory, but I can't seem to tell what the problem is. Here's my code:

File folder = new File(Environment.getExternalStorageDirectory() + "/folder");
folder.mkdir();

I have also edited my manifests file to add this:

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

Also, I have no duplicates of the folder either.

Help would be great! Thanks!

Ian
  • 25
  • 9
  • "my code is simply not creating a directory" -- how are you determining this? If the answer is anything other than `adb shell ls`, bear in mind that the directory may not show up until you put a file in it and [get that file indexed by the `MediaStore`](https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl). – CommonsWare Dec 30 '16 at 22:36
  • I am using this code: boolean result = folder.mkdirs(); Log.d("Directory made:" + result); – Ian Dec 30 '16 at 22:38
  • Possible duplicate of [how to create a folder in android External Storage Directory?](http://stackoverflow.com/questions/24781213/how-to-create-a-folder-in-android-external-storage-directory) – Oleg Bogdanov Dec 30 '16 at 22:41
  • No, I have no duplicates – Ian Dec 30 '16 at 22:46
  • First, `mkdir()` will return `false` if the directory is already there. Beyond that, replace `new File(Environment.getExternalStorageDirectory() + "/folder")` with `new File(Environment.getExternalStorageDirectory(), "folder")`, add [runtime permission support](https://developer.android.com/training/permissions/requesting.html), and use `adb shell` to determine if your directory is actually there. – CommonsWare Dec 30 '16 at 22:48

1 Answers1

2

Try this code

File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"images");
directory.mkdirs();

Reference here

Community
  • 1
  • 1
shivampip
  • 2,004
  • 19
  • 19