I’m trying to load a JSON file from my ResourcesDirectory on Android. I’m aware that there are some restrictions but my code is pretty straightforward.
[lua]
local task=getTask({
path=“data”,
baseDir=system.ResourceDirectory
},“practice”)
function getTask(pathParam,folder)
local path=toPath(pathParam)
path=path…"/"…folder…"/task.json"
local fh,err=io.open(path,“r”)
if not err then
local contents=fh:read("*a")
fh:close()
local task=json.decode(contents)
end
assert(not err,err)
…
end
function toPath(param)
local baseDir=param.baseDir
assert((baseDir~=nil) ~= param.isAbsolute,“Path cannot be absolute and in system directory”)
if not param.isAbsolute then
return system.pathForFile(param.path,baseDir)
end
return param.path
end
[/lua]
Simply put it defines a table object that is converted in this case to a proper path. It appends a folder to the path and looks for a json file at that location. I’ve cut the code down for clarity (the actual codebase has to run on desktop as well) hence the support for absolute paths (the user can enter them manually in that environment).
When running on an Android device I get an error: /data/data/bundle/file/coronaResources/data/practice/task.json: No such file or directory. However if I unzip the apk I can see the JSON file at assets/data/practice/task.json! What am I doing wrong? The code runs fine on iOS and Windows desktop.