Android Question File.MakeDir making unwritable folders

techgreyeye

Member
Licensed User
Longtime User
Hi All,

I've been implementing data backup for my app and have some strange results. Has anyone seen this behaviour before?

I create a folder using File.MakeDir and try to copy a file into it. I then get the following exception :

B4X:
(Exception) java.lang.Exception:  java.io.FileNotFoundException: /storage/emulated/0/Android/data/gtl.stockmate/files/out5/hh_auth.txt: open failed: ENOENT (No such file or directory)

Further examination revealed that the folder is created and the permissions look correct (using ES File Explorer), but somehow this folder is not writeable. I can't copy a file into it at all, either in my app, or ES File Explorer. The folder is somehow corrupt.

So... I implemented the following bit of code :

B4X:
   Dim created As Boolean
   Dim retries As Int
   retries = 0
   created = False
   Do While Not (created)
    Try
     File.MakeDir(Main.Home_Folder, "out" & outid)
     File.Copy(Main.Home_Folder, "hh_auth.txt", Main.Home_Folder & "/out" & outid, "hh_auth.txt")
     created = True
    Catch
     Log(LastException)
     retries = retries + 1
     created = False
     If File.Exists(Main.Home_Folder, "out" & outid) Then
      File.Delete(Main.Home_Folder, "out" & outid)
     End If
    End Try
   Loop
  If retries > 0 Then
   Msgbox("Retries - " & retries, "Error")
  End If

The results of running this repeatedly are basically after a random number of retries
(16,14,8,0,0,19,10,5,0,20 in a run of ten tests), the folder is successfully created and the file copied in.

This only seems to happen when creating a folder which previously existed (it get's deleted earlier in the backup process). The retries above are for a folder which previously contained 10 files.

I re-tested using a folder which contained 70 files and had the following number of retries -
133,246,171,244,276,159,246,251,232,232

This is hinting that it's related to file allocation tables, but I know very little about filesystems so....


Has anyone had a similar issue, and can suggest what's might be going on and/or workarounds smarter then retrying lots of times?


Thanks!!
 

techgreyeye

Member
Licensed User
Longtime User
Further info -

I retested on a couple of other devices, and this is happening on both my android 5.0.2 devices (ASUS P01Z and Nexus 7) but not my Android 7 phone.
 
Upvote 0

techgreyeye

Member
Licensed User
Longtime User
Hi buras,

The folder is within the apps default storage so doesn't need permissions. I have no general problem creating folders, just issues when I delete then immediately re-create a folder.

Thanks
 
Upvote 0

buras3

Active Member
Licensed User
Longtime User
hi
i have tested your code and i didn't get any message
i tested with 1 file only
but i saw this
upload_2018-11-2_16-7-32.png
 
Upvote 0
Top