Traversing your project directory with Lua File System

Hi all,

I am trying to create a little widget that allows the user to cycle through different options, each represented by an image.  To do this, I need to use lfs to search a given directory for images to cycle through.  Here is the code I am using to do this:

for file in lfs.dir (path_for_elements) do
     if (file ~= “.”) and (file ~= “…”) then
          t[i] = path_for_elements…file

          i = i + 1
     end
end

This works in the simulator, but not on an Android build.  I am assuming that this is because the file structure is compiled into binary and can’t be traversed with the lfs library.

Right now, I am working around this by mirroring the structure of my images directory with a table.

I believe, that the resource directory (where your app and assets live) does not exist in a traversable format on Android.

So, you won’t be able to list files and folders there.  You can only give direct paths within your code prior to building.  This is not Corona’s fault, but the way Android binaries work.

However, since you’ve already got all of your images and path data when you make your app, simply put that in a table and use that.  i.e. Don’t discover the images at run time and you’ll be fine.

-Ed

Note: If you are completely in love with the idea of discovering paths and files, you can also do this:

1.Have your app detect whether it is ‘in the simulator’ or on device.

2a. When ‘in simulator’ discover all paths and files.  

2b. Then, save that info in a file as text or as a JSON encoded table.

2c. Save the file.

  1. When you go to build, copy that file over to your resource directory and have your app read from it when running on device.

Viola!  Discoverable for development, fixed and locateable when running on device.  

I believe, that the resource directory (where your app and assets live) does not exist in a traversable format on Android.

So, you won’t be able to list files and folders there.  You can only give direct paths within your code prior to building.  This is not Corona’s fault, but the way Android binaries work.

However, since you’ve already got all of your images and path data when you make your app, simply put that in a table and use that.  i.e. Don’t discover the images at run time and you’ll be fine.

-Ed

Note: If you are completely in love with the idea of discovering paths and files, you can also do this:

1.Have your app detect whether it is ‘in the simulator’ or on device.

2a. When ‘in simulator’ discover all paths and files.  

2b. Then, save that info in a file as text or as a JSON encoded table.

2c. Save the file.

  1. When you go to build, copy that file over to your resource directory and have your app read from it when running on device.

Viola!  Discoverable for development, fixed and locateable when running on device.