Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Understanding lfs.dir() on Android
Started by sergio.rodrigo.royo Mar 12 2019 08:24 AM

4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

sergio.rodrigo.royo

[GLOBAL: userInfoPane.html]
sergio.rodrigo.royo
  • Observer

  • 3 posts
  • Corona SDK

Hi, I've got a question.

 

I've got this code that reads all the files in a folder (at the root of the project) and does some stuff with them:

local levelsFile, errorString = io.open(system.pathForFile("migrations/levels/levels-001.json"), "r")
io.close(levelsFile)
levelsFile = nil

-- Read all files in the folder
local migrationsPath = system.pathForFile("migrations/levels")
for migrFilename in lfs.dir(migrationsPath) do
    -- Read the file and do stuff with it. It doesn't matter for the question
end

This code works as it is on an Android device. I had to add the section that says "Read one of the files in the folder", otherwise the lfs.dir statement would work on the simulator but not on the Android device (would say the folder doesn't exist). I made a non-live build, installed the .apk as usual.

I wonder why this is behaving in this way. I came with this "solution" by exhaustion, after trying countless things, and it doesn't make sense at all to me, I shouldn't need to open a file in the folder before being able to list the files in that folder. This is definitely reading from system.ResourceDirectory, which I know it behaves in a particular way on Android but still weird.

Any insights appreciated.



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 582 posts
  • Corona SDK

Hey!

I am having some difficulty understanding what you are after.

What is the old version of the code that you tried that didn't work? Also, how did it not work, e.g. did your app encounter a runtime error with a message along the lines of "bad argument #1 to 'dir' (string expected, got nil)", or did something else happen? It'd be useful to know what you tried, what you wanted to happen and what actually happened.

Like you said, you don't need to open a file within a folder first to read through the folder's contents, for instance, you can just run the code below to print out a list of all files and folders inside of your app's ResourceDirectory. No opening or closing of other files needed.
 

local lfs = require( "lfs" )
local doc_path = system.pathForFile( "" )

for file in lfs.dir( doc_path ) do
    print( "Found file: " .. file )
end


[TOPIC: post.html]
#3

sergio.rodrigo.royo

[GLOBAL: userInfoPane.html]
sergio.rodrigo.royo
  • Observer

  • 3 posts
  • Corona SDK

Hi!

Thanks for answering :)

I didn't explain myself very well on my previous message.
The code I tried originally was this:
 

-- Read all files in the folder
local migrationsPath = system.pathForFile("migrations/levels")
for migrFilename in lfs.dir(migrationsPath) do
    -- Read the file and do stuff with it. It doesn't matter for the question
end

That alone didn't work. The error was what you say, "bad argument #1 to 'dir' (string expected, got nil)".

Then, I tried to read a single file in that folder before, just for debugging to make sure Corona could find any files at all in that folder, and then lfs.dir didn't throw any errors anymore, which is the original code I posted. Really weird.



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 582 posts
  • Corona SDK

That error would seem to suggest that the folder itself doesn't exist. Are you on Windows or Mac?

I'm not 100% sure about Mac, but Windows isn't case sensitive (I don't think that Mac is either). If the file's or folder's name doesn't match exactly, then on Windows at least you would see a warning in your console with something like: "WARNING: case of filename 'C:\path\filename' differs on disk" If you see that, then this would crash your app on Android.

Still, I have absolutely no idea as to why opening and closing a file in the same folder would prevent the crash.



[TOPIC: post.html]
#5

sergio.rodrigo.royo

[GLOBAL: userInfoPane.html]
sergio.rodrigo.royo
  • Observer

  • 3 posts
  • Corona SDK

I don't think it's related to Mac or Windows. As described on my original comment, the problem only happens on a real Android phone; it works fine on the simulator without any warnings.

 

 

 

Still, I have absolutely no idea as to why opening and closing a file in the same folder would prevent the crash.

I don't have any idea either, it's really strange.




[topic_controls]
[/topic_controls]