Sagar Maiyad
Sagar Maiyad

Reputation: 12753

Folders not created using mkdirs() in android

I am creating folders using mkdirs() method. Before that, i am also checking if folder is already present or not. if not then only create. It works fine almost all devices but somehow its not creating in some devices.

I also checked runtime permission and storage access framework before creating folders. its all good still it does not create folders. Below is the sample path of creating folders:

Path: /storage/emulated/0/MyAppFolder/TestFolder

Here, /storage/emulated/0/ is internal storage path. After that i am creating two folders using below code:

val folder = File(Path)
if (!folder.exists()) {
   if(!folder.mkdirs()){
     Log.e("MyActivity","Folder not created")
   }
}

I also tried using below code:

val folder = File(Path)
if (!folder.parentFile.exists()) {
   if(!folder.parentFile.mkdirs()){
     Log.e("MyActivity","Folder not created")
   }
}

But still not working.

Upvotes: 0

Views: 225

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200130

As per the getFilesDir() documentation, you should never assume a hardcoded installation or directory path - you should only be using relative paths compared to one of the storage directories

Upvotes: 2

Related Questions