Reading files from system.DocumentsDirectory on device doesn't work

Hi,

When I run the app from the simulator I can download images, I can find them on the Documents folder in the Sandbox and after that the images are displayed correctly however not on the device.

Device: Samsung Galaxy S4

Android version: 4.3

Error message:

I/Corona &nbsp;( 5404): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:194: attempt to index upvalue 'currentImage' (a nil value) I/Corona ( 5404): Runtime error I/Corona ( 5404): I/Corona ( 5404): stack traceback: I/Corona ( 5404): [C]: ? I/Corona ( 5404): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:194: in function 'drawNextImage' I/Corona ( 5404): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:91: in function 'nextWord' I/Corona ( 5404): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:504: in function \<...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:431\> I/Corona ( 5404): ?: in function 'dispatchEvent' I/Corona ( 5404): ?: in function 'gotoScene' I/Corona ( 5404): ...s\Corona Projects\SpaPicV4\scene\_main\_categories.lua:266: in function '\_listener' I/Corona ( 5404): ?: in function \<?:141\> I/Corona ( 5404): ?: in function \<?:218\>

As I said, the images are loaded correctly from the simulator.

The images are being downloaded the the beginning and I can see that the size of the application goes from ~12MB to ~32MB, which is correct. There is 200 images being downloaded with a total size of ~20MB.

Is there anyway to actually look at this images from the decive? I look in the Documents folder but it’s empty.

The images are loaded like this:

currentImage = display.newImage(words[wordCounter].imageLocation, system.DocumentsDirectory, 500, 45)

Best regards,

Tomas

Are there any warnings a few lines up in the log?  Could this be a case sensitivity problem?  Devices are case sensitive, the simulator is not.

Rob

Hi Rob,

before I used to deploy the images in the APK and loading them from the location without adding the base directory and that worked fine:

display.newImage(words[wordCounter].imageLocation, 500, 45)

So I don’t think that’s the problem.

Here is the complete log:

--------- beginning of /dev/log/system --------- beginning of /dev/log/main V/Corona (11148): \> Class.forName: network.LuaLoader V/Corona (11148): \< Class.forName: network.LuaLoader V/Corona (11148): Loading via reflection: network.LuaLoader V/Corona (11148): \> Class.forName: CoronaProvider.licensing.google.LuaLoader V/Corona (11148): \< Class.forName: CoronaProvider.licensing.google.LuaLoader V/Corona (11148): Loading via reflection: CoronaProvider.licensing.google.LuaLoader V/Corona (11148): WARNING: Asset file "assets/infosprite@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Fruits@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Animals@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Body@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Education@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Clothes@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Foods@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Games@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Measurements@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Occupations@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Time@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Transports@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Vegetables@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/play@2x.png" does not exist. I/Corona (11148): Runtime error I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:194: attempt to index upvalue 'currentImage' (a nil value) I/Corona (11148): stack traceback: I/Corona (11148): [C]: ? I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:194: in function 'drawNextImage' I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:91: in function 'nextWord' I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:504: in function \<...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:431\> I/Corona (11148): ?: in function 'dispatchEvent' I/Corona (11148): ?: in function 'gotoScene' I/Corona (11148): ...s\Corona Projects\SpaPicV4\scene\_main\_categories.lua:266: in function '\_listener' I/Corona (11148): ?: in function \<?:141\> I/Corona (11148): ?: in function \<?:218\> I/Corona (11148): Runtime error I/Corona (11148): I/Corona (11148): stack traceback: I/Corona (11148): [C]: ? I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:194: in function 'drawNextImage' I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:91: in function 'nextWord' I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:504: in function \<...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:431\> I/Corona (11148): ?: in function 'dispatchEvent' I/Corona (11148): ?: in function 'gotoScene' I/Corona (11148): ...s\Corona Projects\SpaPicV4\scene\_main\_categories.lua:266: in function '\_listener' I/Corona (11148): ?: in function \<?:141\> I/Corona (11148): ?: in function \<?:218\>

All I can find is the nil value for the image.

I have verified the images and all the images are lower case and this is from words[wordCounter].imageLocation:

assets/images/fruits/apple.jpg

I don’t know if it matters if it’s a forward or backward slash? It didn’t when I had the file locally. Maybe it should be an extra slash at the beginning for it to be displayed properly on the device?

Best regards,

Tomas

what is the value of words[wordCounter].imageLocation?

Rob

It’s a value taken from the database:

assets/images/fruits/apple.jpg

Which was the direction to the actual image from where main.lua was (i.e. a folder under the project folder called assets, and then images etc.)

But now I have added to look for this path under system.documentsDirectory and that’s when the problems started.

If I add the images to the project folder and remove the system.documentsDirectory the images are being displayed correctly.

Best regards,

Tomas

Do you have a folder in your system.DocumentsDirectory called assets that has a folder called images that has a folder called fruits that has apple.jpg in it?  If these exist in your system.ResourcesDirectory (where your main.lua lives) that would explain why its not working. You have to do something to put those images in system.DocumentsDirectory (download them from online, copy them from system.ResourcesDirectory etc.)

Rob

Hi Rob,

yes, they are there. They are being downloaded from a server and they are being downloaded to the Documents folder in the Sandbox folder (this is in the simulator) and I can see that all the images and all the folders exists. Here are a couple of images for example:

http://i42.tinypic.com/2cpc49i.png

And here is the image being displayed in the simulator:

http://i39.tinypic.com/10nhtfd.png

So it works in the simulator - the images are taken from the Documents folder.

Is there anyway to see the actual images (the files) on the device? As I said earlier the size of the app increases from 12 to 32MB after the images have been downloaded so they do exist but they can’t seem to be found. I have seen the problem of the device being case sensitive but in this case the images could be loaded before but not now from the Documents folder so I was wondering if the base directory reads them differently (Maybe a slash at the beginning of imageLocation or something like that)?

So, on the simulator:

  1. Images are being downloaded to the Documents and I can see them i.e. no problem

  2. Images are being displayed on the simulator i.e. no problem

On the decive:

  1. The images are being downloaded but I can’t find them under the Document folder (not sure if it’s the right folder though)

  2. The images are not being displayed, get’s a nil value i.e. can’t be found

This is how the images are being downloaded:

function downloadImage(image) local path = "assets/images/" .. image.category .. "/" local filename = string.lower(image.word:gsub("%s+", "")) .. ".jpg" local params = {} params.progress = true network.download( "http://jefecitostudios.com/languageacademy/"..image.imageLocation, "GET", networkListener, params, path .. filename, system.DocumentsDirectory ) end

Best regards,

Tomas

Did you ever make those intermediate directories?  Did you create them in the sandbox?

Rob

Hi Rob,

Before the images are being downloaded they are being created:

function nw.verifyFolders(categories) local temp\_path = system.pathForFile( "", system.DocumentsDirectory ) lfs.chdir( temp\_path ) lfs.mkdir( "assets" ) lfs.mkdir( "assets/images" ) for i = 1, #categories, 1 do lfs.mkdir( "assets/images/" .. string.lower(categories[i].category) ) end end

So if I delete everything from Documents (under the Sandbox folder) they get created before the images are being downloaded.

Best regards,

Tomas

In your network listener can you dump the event table to see what’s being downloaded?

Rock on!

As you knew from the beginning it was a case sensitive problem.

The folders were created correctly, the download links were correct but when creating the path for the new image to be stored I forgot to add string.lower:

local path = "assets/images/" .. string.lower(image.category) .. "/"

So the categories was always returned as “Fruits”, “Animals” etc.

Thanks Rob!

Best regards,

Tomas

Are there any warnings a few lines up in the log?  Could this be a case sensitivity problem?  Devices are case sensitive, the simulator is not.

Rob

Hi Rob,

before I used to deploy the images in the APK and loading them from the location without adding the base directory and that worked fine:

display.newImage(words[wordCounter].imageLocation, 500, 45)

So I don’t think that’s the problem.

Here is the complete log:

--------- beginning of /dev/log/system --------- beginning of /dev/log/main V/Corona (11148): \> Class.forName: network.LuaLoader V/Corona (11148): \< Class.forName: network.LuaLoader V/Corona (11148): Loading via reflection: network.LuaLoader V/Corona (11148): \> Class.forName: CoronaProvider.licensing.google.LuaLoader V/Corona (11148): \< Class.forName: CoronaProvider.licensing.google.LuaLoader V/Corona (11148): Loading via reflection: CoronaProvider.licensing.google.LuaLoader V/Corona (11148): WARNING: Asset file "assets/infosprite@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Fruits@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Animals@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Body@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Education@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Clothes@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Foods@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Games@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Measurements@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Occupations@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Time@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Transports@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/categories/sprite/Vegetables@2x.png" does not exist. V/Corona (11148): WARNING: Asset file "assets/play@2x.png" does not exist. I/Corona (11148): Runtime error I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:194: attempt to index upvalue 'currentImage' (a nil value) I/Corona (11148): stack traceback: I/Corona (11148): [C]: ? I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:194: in function 'drawNextImage' I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:91: in function 'nextWord' I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:504: in function \<...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:431\> I/Corona (11148): ?: in function 'dispatchEvent' I/Corona (11148): ?: in function 'gotoScene' I/Corona (11148): ...s\Corona Projects\SpaPicV4\scene\_main\_categories.lua:266: in function '\_listener' I/Corona (11148): ?: in function \<?:141\> I/Corona (11148): ?: in function \<?:218\> I/Corona (11148): Runtime error I/Corona (11148): I/Corona (11148): stack traceback: I/Corona (11148): [C]: ? I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:194: in function 'drawNextImage' I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:91: in function 'nextWord' I/Corona (11148): ...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:504: in function \<...to\Documents\Corona Projects\SpaPicV4\scene\_game.lua:431\> I/Corona (11148): ?: in function 'dispatchEvent' I/Corona (11148): ?: in function 'gotoScene' I/Corona (11148): ...s\Corona Projects\SpaPicV4\scene\_main\_categories.lua:266: in function '\_listener' I/Corona (11148): ?: in function \<?:141\> I/Corona (11148): ?: in function \<?:218\>

All I can find is the nil value for the image.

I have verified the images and all the images are lower case and this is from words[wordCounter].imageLocation:

assets/images/fruits/apple.jpg

I don’t know if it matters if it’s a forward or backward slash? It didn’t when I had the file locally. Maybe it should be an extra slash at the beginning for it to be displayed properly on the device?

Best regards,

Tomas

what is the value of words[wordCounter].imageLocation?

Rob

It’s a value taken from the database:

assets/images/fruits/apple.jpg

Which was the direction to the actual image from where main.lua was (i.e. a folder under the project folder called assets, and then images etc.)

But now I have added to look for this path under system.documentsDirectory and that’s when the problems started.

If I add the images to the project folder and remove the system.documentsDirectory the images are being displayed correctly.

Best regards,

Tomas

Do you have a folder in your system.DocumentsDirectory called assets that has a folder called images that has a folder called fruits that has apple.jpg in it?  If these exist in your system.ResourcesDirectory (where your main.lua lives) that would explain why its not working. You have to do something to put those images in system.DocumentsDirectory (download them from online, copy them from system.ResourcesDirectory etc.)

Rob

Hi Rob,

yes, they are there. They are being downloaded from a server and they are being downloaded to the Documents folder in the Sandbox folder (this is in the simulator) and I can see that all the images and all the folders exists. Here are a couple of images for example:

http://i42.tinypic.com/2cpc49i.png

And here is the image being displayed in the simulator:

http://i39.tinypic.com/10nhtfd.png

So it works in the simulator - the images are taken from the Documents folder.

Is there anyway to see the actual images (the files) on the device? As I said earlier the size of the app increases from 12 to 32MB after the images have been downloaded so they do exist but they can’t seem to be found. I have seen the problem of the device being case sensitive but in this case the images could be loaded before but not now from the Documents folder so I was wondering if the base directory reads them differently (Maybe a slash at the beginning of imageLocation or something like that)?

So, on the simulator:

  1. Images are being downloaded to the Documents and I can see them i.e. no problem

  2. Images are being displayed on the simulator i.e. no problem

On the decive:

  1. The images are being downloaded but I can’t find them under the Document folder (not sure if it’s the right folder though)

  2. The images are not being displayed, get’s a nil value i.e. can’t be found

This is how the images are being downloaded:

function downloadImage(image) local path = "assets/images/" .. image.category .. "/" local filename = string.lower(image.word:gsub("%s+", "")) .. ".jpg" local params = {} params.progress = true network.download( "http://jefecitostudios.com/languageacademy/"..image.imageLocation, "GET", networkListener, params, path .. filename, system.DocumentsDirectory ) end

Best regards,

Tomas

Did you ever make those intermediate directories?  Did you create them in the sandbox?

Rob

Hi Rob,

Before the images are being downloaded they are being created:

function nw.verifyFolders(categories) local temp\_path = system.pathForFile( "", system.DocumentsDirectory ) lfs.chdir( temp\_path ) lfs.mkdir( "assets" ) lfs.mkdir( "assets/images" ) for i = 1, #categories, 1 do lfs.mkdir( "assets/images/" .. string.lower(categories[i].category) ) end end

So if I delete everything from Documents (under the Sandbox folder) they get created before the images are being downloaded.

Best regards,

Tomas

In your network listener can you dump the event table to see what’s being downloaded?