Reputation: 113
I'm trying to create a new folder in the external storage of the emulator. It's working once, but not in the second time. I tried debugging, and this is the debug log :
>> Let's debug why this directory isn't being created:
Is it working?: false
Does it exist?: false
What is the full URI?: file:/storage/emulated/0/Notepad/Apr%203,%202018%2012:27:46%20PM
--
Can we write to this file?: false
>> We can't write! Do we have WRITE_EXTERNAL_STORAGE permission?
We do have permission - the problem lies elsewhere.
Are we even allowed to read this file?: false
--
>> End of debugging.
And this is the error :
java.io.FileNotFoundException: /storage/emulated/0/Notepad/Apr 3, 2018 12:27:46 PM/database: open failed: ENOENT (No such file or directory)
It's working well on API 23 and API 27, but it's failing on API 17 and API 19.
What could be wrong? Why isn't the folder being created? I have read and write permission, so I'm sure that's not the error, it's working on API 23.
Edit
This is the code before the log
val backupFolder = File(Environment.getExternalStorageDirectory(), "Notepad")
if (!backupFolder.exists()) backupFolder.mkdir()
val date = SimpleDateFormat.getDateTimeInstance().format(System.currentTimeMillis())
val newBackupFolder = File(backupFolder, date)
newBackupFolder.mkdir()
and this is after the log :
val backupFile = File(newBackupFolder, "database")
databaseFile.copyTo(backupFile)
Upvotes: 1
Views: 271
Reputation: 13865
You are creating a folder with :
present in the path.
This to my knowledge is an invalid character. It is definitely invalid on Windows. (try creating a folder with a colon, and you will see the warning Windows throws)
Try formatting the date part to use .
(full stops) instead. As visually it should be similar, and is a valid character.
Try a format like below for a directory friendly format:
new SimpleDateFormat("yyyy-MM-dd HH.mm.ss")
Upvotes: 1