Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Cant read a .json file from system.ResourceDirectory
Started by aleksander.gogoc Mar 20 2020 11:33 AM

2 replies to this topic
file android read resourcedirectory
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

aleksander.gogoc

[GLOBAL: userInfoPane.html]
aleksander.gogoc
  • Observer

  • 2 posts
  • Corona SDK

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(fileNamesystem.ResourceDirectory)
if(mainLanguageFilePath == nilthen
  mainLanguageFilePath = system.pathForFile(nilsystem.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 == nilthen
  mainLanguageFilePath = system.pathForFile(nilsystem.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.



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,246 posts
  • Corona SDK

Have you checked if the filename is written correctly? Android is cAsE sEnSiTiVe while Windows, for isn't.

Also, it is a good practice in general to first check if a file exists before you try to load it. Otherwise it'll always crash, as is the case here. It's always better to let your users know that something failed via a notification than via a crash.

 

Example from documentation:

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
 
-- Open the file handle
local file, errorString = io.open( path, "r" )
 
if not file then
    -- Error occurred; output the cause
    print( "File error: " .. errorString )
else
    -- Read data from file
    local contents = file:read( "*a" )
    -- Output the file contents
    print( "Contents of " .. path .. "\n" .. contents )
    -- Close the file handle
    io.close( file )
end
 
file = nil


[TOPIC: post.html]
#3

aleksander.gogoc

[GLOBAL: userInfoPane.html]
aleksander.gogoc
  • Observer

  • 2 posts
  • Corona SDK

Thanks, XeduR @Spyric

 

Indeed it was a problem with the filename. I've resolved it a little bit different, by adding a line:

if(path ~= nilthen <<use another file>>



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: file, android, read, resourcedirectory