Hi, I’m facing a problem with reading a file. I’ve read all documentation and forums posts I’ve found, but none of them actually helps, and I think it’s something very simple.
I’ve placed a file “en_US.json” in a Corona project folder (same as “main.lua”). Then I want to read this file in a runtime:
local fileName = “en_US.json”
mainLanguageFilePath = system.pathForFile(fileName, system.ResourceDirectory)
if(mainLanguageFilePath == nil) then
mainLanguageFilePath = system.pathForFile(nil, system.ResourceDirectory) … “/” … fileName
end
print("Attempting to open a file: " … mainLanguageFilePath)
file = io.open(mainLanguageFilePath, “r”)
local contents = file:read("*a")
io.close(file)
stringDictionary = json.decode(contents)
It works correctly in Corona Simulator, but it is not working on Android phone. I understand that system.ResourceDirectory should be equivalent of application .zip file, and some files can be read from there only by specific APIs. But .json file should be possible to read (I’m not trying to write anything into that file).
These lines are for Corona Simulator, without that it’s also not working on Simulator:
if(mainLanguageFilePath == nil) then
mainLanguageFilePath = system.pathForFile(nil, system.ResourceDirectory) … “/” … fileName
end
but on Android I have an error in io.open() (passing a nil value).
Eventually I’d like to create a subdirectory for language files in system.ResourceDirectory, but I don’t know how to read those files on Android.