Search code examples
androidfileio

Android File.createNewFile false but exists also false


Really strange issue, i'm using SimpleStorage but i've gone down to the actuall IO, i'm using Android 11 heres the issue:

  1. createNewFile in app and write it to downloads folder etc., no problem
  2. open samsung filemanager, delete file
  3. repeat step 1: createNewFile on same path/filename

step 1 works without a problem (rules out access/permission issue), but at step 3 createNewFile returns false and if i debug i get these evaluated return values

file.length = 0
file.isEmpty = false
file.isFile = false
file.createNewFile = false
file.exists = false

this seems not to make much sense, the file has a 0 length but isnt empty? it doesnt exist but cant be created?

Also its really about the filename, if i rename the file and try again it works, if i rename another file to the filename of a deleted file it doesnt work.


Solution

  • Can confirm what you found.

    I deleted the file on an Android 11 defices with the official Files app.

    Could not create a file with the same name again.

    Until i obtained All Files Access for the app. Then i could recreate the file.

    Mostly on an Android 11 device if the file exists() but you cannot File.list() it and not read or write it then the MediaStore still has an entry for the file. So then you have to use the media store to delete the entry first.

    But today the test file was nowhere to be found in the media store so i'm puzzled too.