system.ResourceDirectory only works on iOS, not on Android.
CoronaLabs have implemented a limited “hack” to get around this though… read “Gotchas” in the following link:
http://docs.coronalabs.com/api/library/system/ResourceDirectory.html
system.ResourceDirectory only works on iOS, not on Android.
CoronaLabs have implemented a limited “hack” to get around this though… read “Gotchas” in the following link:
http://docs.coronalabs.com/api/library/system/ResourceDirectory.html
Thanks for the tip, however I will try to arrange an alternative solution to check for files.
greetings
Hi there,
You’re missing a “/” when appending the folder onto the path. It should be:
doc_path = doc_path…"/images/"…localProperties.imageDir
Also you need local lfs = require(“lfs”) at the top (if you don’t already have that). Otherwise it won’t run on an actual device.
However, this code will only work when run on a device. It will not work in the Corona Simulator as the returned Resource directory points to the Corona Simulator as opposed to your app. I think this is a bug which I’m going to report.
Bug report filed as Case 27590.
I was under the impression that Corona couldn’t access the local file system of a device. I’m not near my machine to test this code; please let us know how you get on.
Yeah, I forgot about Android… The code will only work on iOS.
More info here:
http://docs.coronalabs.com/api/library/system/ResourceDirectory.html
thanks for the response. Unfortunately it still is not working on device for me. I’m using the logger in xcode while I run the app, and when that code gets executed my app crashes on the device and there are no helpful messages in the logger. =(
I did have the print statement for my path and it says “/var/mobile/Applications/numbers&symbols here/myAppName.app/image/exteriorDoors”
As far as I can tell that path is what I am looking for. The exteriorDoors folder just has images inside of it, and that is the folder I wanted to scan for files. Any other ideas?
I found this info, but again I can’t really test it:
http://stackoverflow.com/questions/8443291/how-to-load-images-from-sdcard-in-corona-sdk
@panc software,
Thanks but that is a different issue than what I am looking at. I am working with an IOS only app and also just trying to read from the system.ResourcesDirectory.
I have found a post in stack overflow of a guy that seems to have found a solution, but I can’t get it to work. here it is:
"After much debugging I have figured out the solution for Mac. Below is my function for getting files in a directory. Works for both Windows and Mac. Must have a file named main.lua in the baseDirectory. -- path is relative to the baseDirectory function getFiles(path) local filenames = { } local baseDir = system.pathForFile('main.lua'):gsub("main.lua", "") for file in lfs.dir(baseDir .. path) do if (file ~= '.' and file ~= '..') then table.insert(filenames, file) end end if (#filenames == 0) then return false end return filenames end The problem is system.pathForFile('directory/subdirectory') does not work on Mac as it is strictly looking for a file. As long as it can find some file in the baseDirectory you can append on the relative path to retrieve all files in the intended directory."
I have attempted to use the above code but get an error on my path like always. When he says that the “baseDirectory” must have a file named “main.lua”, does that mean my “main.lua” in the system.ResourcesDirectory should work?
EDIT: This solution works on MAC but fails on my actual iPAd, still trying to figure it out…
When calling it I just passed a string of the directory path instead of trying to pass it what “system.pathForFile” would return.
For example:
local thisPath = "images/"..subDirectoryName local theFiles = getFiles(thisPath)
It then returns a table to “theFiles”, which you can iterate through to find your file names. Hope this helps someone else as well.
Ok, maybe someone can answer this question for me. Why is it that if I attempt the following, I get nil?
local thisDir = system.pathForFile("main.lua", system.ResourcesDirectory) print(thisDir) --Outputs nil
I can’t believe that I can’t even get a path to the main root of the resources directory?! This is on device, what is wrong here?
I officially cannot get a path to a file found in the system.ResourcesDirectory on device. I can however get a path to anything in the system.DocumentsDirectory.
Is this perhaps a bug in IOS7?
The reason why the code you found doesn’t work on device is because there is no “main.lua” in the resource directory after compiling to device (it’s there when you run in the Corona Simulator though).
But… Your original code that you posted *does* work on device (with the minor path-modification I mentioned). I tested it myself on my iPad mini and iPod Touch. If I had to guess, I’d say that maybe the “&” in the path name is tripping something up…
Hey… wait a minute!
You’ve mis-spelled a part of your code. It should be system.ResourceDirectory not system.ResourcesDirectory :).
If you use your original code and just change that (and add the extra “/”) it will work on a device.
Corona simulator will still fail due to what I think is a bug (which I’ve reported).
Nice catch! And thanks for reminding me to make that small fix with a “/”. That finally did work. I got so mixed up with all the different methods I was trying to do that I got lost and thought nothing would work.
Thank you very much for your help, saved my sanity!
Great news Glad I could help…
Hello everyone!
I have a question, where should I add this extra “/” ??
Eg I have a function we check lfs in a given path that is received by parameter is a directory, and returns true or false. If true then return through the lfs.dir (path) traverses the path and checks the existence of images.
Where should I add the extra “/”?
Sorry for my bad english…
That depends on how your function returns the directory. Just make sure all directories are separated by “/”.
The example from the OP had some code that did this: doc_path = doc_path…“images/”…localProperties.imageDir
The code needed an extra “/” like this: doc_path = doc_path…"/images/"…localProperties.imageDir
Wait, now I got another doubt, this extra “/” will enable use in lfs is only for android or IOS? Maybe he figured wrong, because what I really wanted was to use lfs for android because I already can for IOS.
Needed to access files that are in the resource (Android).