This application encountered a Lua error (see logs) or has been corrupted

I keep getting this error and the only line causing it is this:

local path = system.pathForFile( nil, system.ResourceDirectory) .. "/levels" 

and I use it with

for file in lfs.dir( path ) do     local str     local match = "level%d+.xml"     if string.find(file, match) then         local lid = string.sub(file, string.find(file, "%d+"))         xmlLevelData = xmlapi:loadFile("levels/level" .. lid .. ".xml" )         data:addLevel(lid, xmlLevelData.child[1].value, xmlLevelData.child[3].value, xmlLevelData.child[4].value, xmlLevelData.child[6].value)     end end

Works perfectly in the simulator, but get it when I start the application on my device, Android.

Try this:

local path = system.pathForFile( “/levels” , system.ResourceDirectory)

2013-05-08 07:39:15.822 Corona Simulator[9501:707] ERROR: The resource file (/levels) could not be found at case-sensitive path (/Users/danaudne/Documents/Projects/risse//levels).

2013-05-08 07:39:15.822 Corona Simulator[9501:707] WARNING: Cannot create path for resource file ‘/levels’. File does not exist.

2013-05-08 07:39:15.823 Corona Simulator[9501:707] Runtime error

/Users/danaudne/Documents/Projects/risse/main.lua:19: bad argument #1 to ‘dir’ (string expected, got nil)

stack traceback:

    [C]: ?

    [C]: in function ‘dir’

    /Users/danaudne/Documents/Projects/risse/main.lua:19: in main chunk

This is what I get in the terminal. I’m on a Mac.

It seems to give me no errors on the PC simulator, but I still got the “This application encountered…” on my phone

Damn, seems like it will not work on Android.

Note:  system.ResourceDirectory  will not work on Android devices because the Resource directory is not really part of the file system.

So is there a way to get all my maps in a folder that I can read from?

Hi Dan,

I assume you’ve read the full guide on Reading and Writing Files?

http://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Where are your maps coming from? Are you generating these on-the-fly, or are you bundling these along with the app? If you’re generating them on the fly, you can’t save/move them to the Resource directory, period… iOS or Android. In that case, you must rely on on the Documents directory or Temporary/Caches (I suggest Documents unless you’re OK with the system potentially removing these without warning).

If you’re bundling them with the app (as in, including them in the core directory), they will reside in the Resource directory, and you can read them as expected (but not overwrite them, of course).

Regards,

Brent

Yes, I build the app with the files in the project. I have a database I added to the Documents folder and that is working perfectly.

But it won’t work when I build it for android, I get the message as in the topic. And I’m only reading the xml files and collecting information from it. And the line of code I got from Rob did not work on Mac simulator, only Windows and not on android.

Thanks for all the help so far.

Hi Dan,

I think maybe the initial slash “/” shouldn’t be in there. When I create a path for a file from my Resource directory, I just do this:

[lua]

local filePath = system.pathForFile( “levels/myFile.txt” )

[/lua]

As for reading something you created in-app and saved to Documents, it would be this:

[lua]

local wdfilePath = system.pathForFile( “myFile.txt”, system.DocumentsDirectory )

[/lua]

But of course, that doesn’t take into account a subfolder. You can create subfolders in the Documents directory, but you have to do so in advance using LFS. You can’t just directly state it in the path file and expect the folder to be created.

I think this is what you’re asking, but maybe I’m not clear on what files you’re putting where (or want to put in a specific folder or subfolder).

Brent

I have my folder risse, this is where the whole project is.

In a subfolder called levels I have a couple of xml files that are the levels.

What I want to do is to go through all the files that match the pattern I want, and then open the files and read some of the information in the files.

Can I not list the files like this when the project is packed in .apk file? Do I need to know what files I want to open, to open them?

Sorry for my bad english.

Edit

2013-05-08 21:40:41.825 Corona Simulator[9501:707] .

2013-05-08 21:40:41.826 Corona Simulator[9501:707] …

2013-05-08 21:40:41.826 Corona Simulator[9501:707] level1.xml

2013-05-08 21:40:41.826 Corona Simulator[9501:707] level1_1.jpg

2013-05-08 21:40:41.826 Corona Simulator[9501:707] level1_2.jpg

2013-05-08 21:40:41.827 Corona Simulator[9501:707] level2.xml

is what I get back when I use the simulator on my Mac

local path = system.pathForFile( nil, system.ResourceDirectory) .. "/levels" for file in lfs.dir( path ) do     print(file) end

Hi Dan,

This should be working on Android. Can you test it on your device, and print the file names as text objects to the screen to check if they’re being read properly on the device build?

Brent

05-10 09:22:46.284: I/ActivityThread(15120): Pub no.whg.risse.files: com.ansca.corona.FileContentProvider

05-10 09:22:46.309: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/liblua.so 0x41bd6438

05-10 09:22:46.309: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/liblua.so 0x41bd6438

05-10 09:22:46.309: D/dalvikvm(15120): No JNI_OnLoad found in /data/data/no.whg.risse/lib/liblua.so 0x41bd6438, skipping init

05-10 09:22:46.309: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libjnlua5.1.so 0x41bd6438

05-10 09:22:46.309: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libjnlua5.1.so 0x41bd6438

05-10 09:22:46.314: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libjnlua5.1.so 0x41bd6438

05-10 09:22:46.314: D/dalvikvm(15120): Shared lib ‘/data/data/no.whg.risse/lib/libjnlua5.1.so’ already loaded in same CL 0x41bd6438

05-10 09:22:46.314: I/dalvikvm(15120): threadid=1: recursive native library load attempt (/data/data/no.whg.risse/lib/libjnlua5.1.so)

05-10 09:22:46.319: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libopenal.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libopenal.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libmpg123.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libmpg123.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): No JNI_OnLoad found in /data/data/no.whg.risse/lib/libmpg123.so 0x41bd6438, skipping init

05-10 09:22:46.319: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libvorbisidec.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libvorbisidec.so 0x41bd6438

05-10 09:22:46.339: D/dalvikvm(15120): No JNI_OnLoad found in /data/data/no.whg.risse/lib/libvorbisidec.so 0x41bd6438, skipping init

05-10 09:22:46.339: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libalmixer.so 0x41bd6438

05-10 09:22:46.339: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libalmixer.so 0x41bd6438

05-10 09:22:46.339: D/dalvikvm(15120): No JNI_OnLoad found in /data/data/no.whg.risse/lib/libalmixer.so 0x41bd6438, skipping init

05-10 09:22:46.339: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libcorona.so 0x41bd6438

05-10 09:22:46.349: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libcorona.so 0x41bd6438

05-10 09:22:51.059: W/WindowManager(2119): Force clearing freeze: AppWindowToken{4244b860 token=Token{42e0c450 ActivityRecord{425530c8 no.whg.risse/com.ansca.corona.CoronaActivity}}}

This is the logcat from when I start up the application.

I still get the same error message on the screen as usual on my phone. And in the simulator I got to print out all the files to the textobject and view it on the screen.

Here’s the .apk file

http://weheartgaming.com/risse.apk

Try this:

local path = system.pathForFile( “/levels” , system.ResourceDirectory)

2013-05-08 07:39:15.822 Corona Simulator[9501:707] ERROR: The resource file (/levels) could not be found at case-sensitive path (/Users/danaudne/Documents/Projects/risse//levels).

2013-05-08 07:39:15.822 Corona Simulator[9501:707] WARNING: Cannot create path for resource file ‘/levels’. File does not exist.

2013-05-08 07:39:15.823 Corona Simulator[9501:707] Runtime error

/Users/danaudne/Documents/Projects/risse/main.lua:19: bad argument #1 to ‘dir’ (string expected, got nil)

stack traceback:

    [C]: ?

    [C]: in function ‘dir’

    /Users/danaudne/Documents/Projects/risse/main.lua:19: in main chunk

This is what I get in the terminal. I’m on a Mac.

It seems to give me no errors on the PC simulator, but I still got the “This application encountered…” on my phone

Damn, seems like it will not work on Android.

Note:  system.ResourceDirectory  will not work on Android devices because the Resource directory is not really part of the file system.

So is there a way to get all my maps in a folder that I can read from?

Hi Dan,

I assume you’ve read the full guide on Reading and Writing Files?

http://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Where are your maps coming from? Are you generating these on-the-fly, or are you bundling these along with the app? If you’re generating them on the fly, you can’t save/move them to the Resource directory, period… iOS or Android. In that case, you must rely on on the Documents directory or Temporary/Caches (I suggest Documents unless you’re OK with the system potentially removing these without warning).

If you’re bundling them with the app (as in, including them in the core directory), they will reside in the Resource directory, and you can read them as expected (but not overwrite them, of course).

Regards,

Brent

Yes, I build the app with the files in the project. I have a database I added to the Documents folder and that is working perfectly.

But it won’t work when I build it for android, I get the message as in the topic. And I’m only reading the xml files and collecting information from it. And the line of code I got from Rob did not work on Mac simulator, only Windows and not on android.

Thanks for all the help so far.

Hi Dan,

I think maybe the initial slash “/” shouldn’t be in there. When I create a path for a file from my Resource directory, I just do this:

[lua]

local filePath = system.pathForFile( “levels/myFile.txt” )

[/lua]

As for reading something you created in-app and saved to Documents, it would be this:

[lua]

local wdfilePath = system.pathForFile( “myFile.txt”, system.DocumentsDirectory )

[/lua]

But of course, that doesn’t take into account a subfolder. You can create subfolders in the Documents directory, but you have to do so in advance using LFS. You can’t just directly state it in the path file and expect the folder to be created.

I think this is what you’re asking, but maybe I’m not clear on what files you’re putting where (or want to put in a specific folder or subfolder).

Brent

I have my folder risse, this is where the whole project is.

In a subfolder called levels I have a couple of xml files that are the levels.

What I want to do is to go through all the files that match the pattern I want, and then open the files and read some of the information in the files.

Can I not list the files like this when the project is packed in .apk file? Do I need to know what files I want to open, to open them?

Sorry for my bad english.

Edit

2013-05-08 21:40:41.825 Corona Simulator[9501:707] .

2013-05-08 21:40:41.826 Corona Simulator[9501:707] …

2013-05-08 21:40:41.826 Corona Simulator[9501:707] level1.xml

2013-05-08 21:40:41.826 Corona Simulator[9501:707] level1_1.jpg

2013-05-08 21:40:41.826 Corona Simulator[9501:707] level1_2.jpg

2013-05-08 21:40:41.827 Corona Simulator[9501:707] level2.xml

is what I get back when I use the simulator on my Mac

local path = system.pathForFile( nil, system.ResourceDirectory) .. "/levels" for file in lfs.dir( path ) do     print(file) end

Hi Dan,

This should be working on Android. Can you test it on your device, and print the file names as text objects to the screen to check if they’re being read properly on the device build?

Brent

05-10 09:22:46.284: I/ActivityThread(15120): Pub no.whg.risse.files: com.ansca.corona.FileContentProvider

05-10 09:22:46.309: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/liblua.so 0x41bd6438

05-10 09:22:46.309: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/liblua.so 0x41bd6438

05-10 09:22:46.309: D/dalvikvm(15120): No JNI_OnLoad found in /data/data/no.whg.risse/lib/liblua.so 0x41bd6438, skipping init

05-10 09:22:46.309: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libjnlua5.1.so 0x41bd6438

05-10 09:22:46.309: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libjnlua5.1.so 0x41bd6438

05-10 09:22:46.314: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libjnlua5.1.so 0x41bd6438

05-10 09:22:46.314: D/dalvikvm(15120): Shared lib ‘/data/data/no.whg.risse/lib/libjnlua5.1.so’ already loaded in same CL 0x41bd6438

05-10 09:22:46.314: I/dalvikvm(15120): threadid=1: recursive native library load attempt (/data/data/no.whg.risse/lib/libjnlua5.1.so)

05-10 09:22:46.319: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libopenal.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libopenal.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libmpg123.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libmpg123.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): No JNI_OnLoad found in /data/data/no.whg.risse/lib/libmpg123.so 0x41bd6438, skipping init

05-10 09:22:46.319: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libvorbisidec.so 0x41bd6438

05-10 09:22:46.319: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libvorbisidec.so 0x41bd6438

05-10 09:22:46.339: D/dalvikvm(15120): No JNI_OnLoad found in /data/data/no.whg.risse/lib/libvorbisidec.so 0x41bd6438, skipping init

05-10 09:22:46.339: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libalmixer.so 0x41bd6438

05-10 09:22:46.339: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libalmixer.so 0x41bd6438

05-10 09:22:46.339: D/dalvikvm(15120): No JNI_OnLoad found in /data/data/no.whg.risse/lib/libalmixer.so 0x41bd6438, skipping init

05-10 09:22:46.339: D/dalvikvm(15120): Trying to load lib /data/data/no.whg.risse/lib/libcorona.so 0x41bd6438

05-10 09:22:46.349: D/dalvikvm(15120): Added shared lib /data/data/no.whg.risse/lib/libcorona.so 0x41bd6438

05-10 09:22:51.059: W/WindowManager(2119): Force clearing freeze: AppWindowToken{4244b860 token=Token{42e0c450 ActivityRecord{425530c8 no.whg.risse/com.ansca.corona.CoronaActivity}}}

This is the logcat from when I start up the application.

I still get the same error message on the screen as usual on my phone. And in the simulator I got to print out all the files to the textobject and view it on the screen.

Here’s the .apk file

http://weheartgaming.com/risse.apk