Corona BUG ALERT - Android file system false results - Notification and Work around provided

With Corona API, to check to see if a file exists, I would normally use this code:

        local fp = system.pathForFile( filename, system.ResourceDirectory)
        if ( fp ) then
            fp = io.open( fp, “r” )
            if ( fp ) then
               – if file exists, code executes here next

               – THIS DOES NOT WORK “RELIABLY” WHEN PLATORM=ANDROID and PATH=system.ResourceDirectory,

               – although it does work for system.DocumentsDirectory and for iOS just fine
            else
               – file does not exist, code would normally execute here next
            end
        end

Thankfully, this bug does not exist for files that are downloaded or created in the system.DocumentsDirectory.

I lost many, many hours seeking to find a solution, convinced that my code was the problem. But in the end, this workaround solved my problem - and I’m 99.999% certain this is a Corona Android BUG that may only become apparent when an App is using extensive file management, as is my new app.

SOLUTION: Because system.ResourceDirectory is the same location as your App, create a text file called “localFiles.txt” and in it, include a list of files you already know are present with the App so that you never have to check if a file exists in system.ResourceDirectory.

Quick way to create localFiles.txt on Windows:

Step 1) Run CMD (to open the old DOS command line)

Step 2) CD /directory of your App

Step 3) dir *.* /s >t.txt

Step 4) load t.txt into a spreadsheet and organize the columns so that you can easily eliminate all columns except those with the file names.

Step 5) another problem is that because Lua can’t search hyphens in filenames, make sure all your filenames either have no hyphens “-”, or you can keep them but replace them with equal signs “=” in localFiles.txt and then convert them back to hyphens at load time in Lua.

Step 6) save file as localFiles.txt

To load localFiles.txt one time at start-up:

function    loadLocalFiles()
    path = system.pathForFile( “localFiles.txt”, system.ResourceDirectory )
    file = io.open( path, “r” )
    V.localFiles = file:read( “*a” )  – global variable

    -----------------------------------------------------

    –    if you  need to convert equal signs “=” back to hyphens “-” after loading localFiles.txt, use this code

        while string.find(V.localFiles,"-")~=nil do    --forgive me if substitute command could do this shorter

              f=string.find(V.localFiles,"-")
              V.localFiles=V.localFiles:sub(1,f-1)…"="…V.localFiles:sub(f+1,string.len(V.localFiles))
        end

    -----------------------------------------------------
    io.close( file )
    file = nil
end

Its very likely you are running into an issue where certain file types in the resource directory are not accessible.  The .apk is just a .zip file.  We unpack files we expect to use, but there are a bunch of file extensions that do not auto extract themselves.  Therefore you can’t just open any file and expect it to work.  I know this is documented somewhere. 

So it’s not a bug, but expected (but very frustrating) behavior.   See:

https://docs.coronalabs.com/guide/data/readWriteFiles/index.html#androidrestrict

Rob

Its very likely you are running into an issue where certain file types in the resource directory are not accessible.  The .apk is just a .zip file.  We unpack files we expect to use, but there are a bunch of file extensions that do not auto extract themselves.  Therefore you can’t just open any file and expect it to work.  I know this is documented somewhere. 

So it’s not a bug, but expected (but very frustrating) behavior.   See:

https://docs.coronalabs.com/guide/data/readWriteFiles/index.html#androidrestrict

Rob