How to read files from system.documentsdirectory on android device

In the main.lua file of my app, i call a function that loads and decodes a json file. The function looks like this:

local function loadJsonFile( filename )  
  
 local path = system.pathForFile (filename, system.DocumentsDirectory)  
 local contents = ""  
 local fileTable = {}  
 local file = io.open ( path, "r" )  
  
 if file then  
  
 local contents = file:read ( "\*a" )   
 fileTable = json.decode ( contents )  
 io.close ( file )  
  
 print ( "loaded" )  
  
 return fileTable  
  
 end  
  
 return nil  
  
end  

In main.lua i call this function like this:

local gameSettings = filefunctions.loadJsonFile( "gamesettings.json" )  

My gamesettings.json file is located in the documents directory of the projects sandbox. After loading and decoding the json file i set some variables like this:

storyboard.gameState.currentscore = gameSettings.CurrentScore  

I do this for each json file in the documents directory ( 3 files). This works perfectly in the simulator ( Windows 7) . When i build for android, install the app on an android device and start it, i get the following runtime error:

…\AppDevelopment\beta\04\EVOLUTION\betaVersion6_0\main.lua:25: attempt to index local gameSettings (a nil value)

Line 25 contains: storyboard.gameState.currentscore = gameSettings.CurrentScore

It just means, opening and decoding the file has failed. My problem is, i don´t understand why - the thing is, everything runs perfectly in the simulator.

Cay anyone help? What is the correct way to handle files on android devices?

[import]uid: 196206 topic_id: 36846 reply_id: 336846[/import]

Hi there,

I think it’s likely to be one of two things.

First, a common cause of errors when something works in the simulator but not on a device is the case sensitivity of file names. In Windows, the simulator is not case sensitive for file names, but iOS and Android devices are. But from what you’ve described, I don’t think that’s the issue.

A second possibility is that the file gamesettings.json actually doesn’t exist in the documents directory on the device, but it does in the sandbox on your computer. Here’s why.

When you build and install an app, the only files from your computer that get included in the build are the files and subfolders where your main.lua is located. All of these files go into the “resource directory” on the device. New files that your app creates go into the “documents directory” (or the temp or cache directory, if you choose to use those in some cases). Importantly, files that were in the documents directory in the simulator sandbox on your computer do not get included in the build that goes on your device.

So, when you install your app to your device, if you immediately try to load a gamesettings.json file from the documents directory, it won’t work, because the documents directory is empty to start.

Typically, what you would do is create a gamesettings.json file in your project folder (the same folder where main.lua is, which is the resource directory). This file will represent the default settings. In your code to load settings, you would first attempt to load a gamesettings.json file from the documents directory. If it doesn’t exist, then you would load the default gamesettings.json file from the resource directory (which you can then save to the documents directory).

  • Andrew

[import]uid: 109711 topic_id: 36846 reply_id: 145137[/import]

Hi Andrew,

thanks a lot for your quick answer. All file names are correct. When i started developing i heard this was a known issue on android devices, so i had an eye on that from the beginning.

The second possibility you mentioned sounds really interesting - i think the problem lies somewhere in that area. I thought, all files from the documents directory within the projects sandbox would be included into the apk file. Most of the articles i read about these i/o issues on android devices pointed out, that there is no resources directory on android devices anyway - so i haven´t thought about the possibility to use the resources directory for those files.

The method you described sounds like the right solution - i will try that.

Thank you very much for your help.

Alex
[import]uid: 196206 topic_id: 36846 reply_id: 145145[/import]

Hi there,

I think it’s likely to be one of two things.

First, a common cause of errors when something works in the simulator but not on a device is the case sensitivity of file names. In Windows, the simulator is not case sensitive for file names, but iOS and Android devices are. But from what you’ve described, I don’t think that’s the issue.

A second possibility is that the file gamesettings.json actually doesn’t exist in the documents directory on the device, but it does in the sandbox on your computer. Here’s why.

When you build and install an app, the only files from your computer that get included in the build are the files and subfolders where your main.lua is located. All of these files go into the “resource directory” on the device. New files that your app creates go into the “documents directory” (or the temp or cache directory, if you choose to use those in some cases). Importantly, files that were in the documents directory in the simulator sandbox on your computer do not get included in the build that goes on your device.

So, when you install your app to your device, if you immediately try to load a gamesettings.json file from the documents directory, it won’t work, because the documents directory is empty to start.

Typically, what you would do is create a gamesettings.json file in your project folder (the same folder where main.lua is, which is the resource directory). This file will represent the default settings. In your code to load settings, you would first attempt to load a gamesettings.json file from the documents directory. If it doesn’t exist, then you would load the default gamesettings.json file from the resource directory (which you can then save to the documents directory).

  • Andrew

[import]uid: 109711 topic_id: 36846 reply_id: 145137[/import]

Hi Andrew,

thanks a lot for your quick answer. All file names are correct. When i started developing i heard this was a known issue on android devices, so i had an eye on that from the beginning.

The second possibility you mentioned sounds really interesting - i think the problem lies somewhere in that area. I thought, all files from the documents directory within the projects sandbox would be included into the apk file. Most of the articles i read about these i/o issues on android devices pointed out, that there is no resources directory on android devices anyway - so i haven´t thought about the possibility to use the resources directory for those files.

The method you described sounds like the right solution - i will try that.

Thank you very much for your help.

Alex
[import]uid: 196206 topic_id: 36846 reply_id: 145145[/import]

Hi there,

I think it’s likely to be one of two things.

First, a common cause of errors when something works in the simulator but not on a device is the case sensitivity of file names. In Windows, the simulator is not case sensitive for file names, but iOS and Android devices are. But from what you’ve described, I don’t think that’s the issue.

A second possibility is that the file gamesettings.json actually doesn’t exist in the documents directory on the device, but it does in the sandbox on your computer. Here’s why.

When you build and install an app, the only files from your computer that get included in the build are the files and subfolders where your main.lua is located. All of these files go into the “resource directory” on the device. New files that your app creates go into the “documents directory” (or the temp or cache directory, if you choose to use those in some cases). Importantly, files that were in the documents directory in the simulator sandbox on your computer do not get included in the build that goes on your device.

So, when you install your app to your device, if you immediately try to load a gamesettings.json file from the documents directory, it won’t work, because the documents directory is empty to start.

Typically, what you would do is create a gamesettings.json file in your project folder (the same folder where main.lua is, which is the resource directory). This file will represent the default settings. In your code to load settings, you would first attempt to load a gamesettings.json file from the documents directory. If it doesn’t exist, then you would load the default gamesettings.json file from the resource directory (which you can then save to the documents directory).

  • Andrew

[import]uid: 109711 topic_id: 36846 reply_id: 145137[/import]

Hi Andrew,

thanks a lot for your quick answer. All file names are correct. When i started developing i heard this was a known issue on android devices, so i had an eye on that from the beginning.

The second possibility you mentioned sounds really interesting - i think the problem lies somewhere in that area. I thought, all files from the documents directory within the projects sandbox would be included into the apk file. Most of the articles i read about these i/o issues on android devices pointed out, that there is no resources directory on android devices anyway - so i haven´t thought about the possibility to use the resources directory for those files.

The method you described sounds like the right solution - i will try that.

Thank you very much for your help.

Alex
[import]uid: 196206 topic_id: 36846 reply_id: 145145[/import]

Hi there,

I think it’s likely to be one of two things.

First, a common cause of errors when something works in the simulator but not on a device is the case sensitivity of file names. In Windows, the simulator is not case sensitive for file names, but iOS and Android devices are. But from what you’ve described, I don’t think that’s the issue.

A second possibility is that the file gamesettings.json actually doesn’t exist in the documents directory on the device, but it does in the sandbox on your computer. Here’s why.

When you build and install an app, the only files from your computer that get included in the build are the files and subfolders where your main.lua is located. All of these files go into the “resource directory” on the device. New files that your app creates go into the “documents directory” (or the temp or cache directory, if you choose to use those in some cases). Importantly, files that were in the documents directory in the simulator sandbox on your computer do not get included in the build that goes on your device.

So, when you install your app to your device, if you immediately try to load a gamesettings.json file from the documents directory, it won’t work, because the documents directory is empty to start.

Typically, what you would do is create a gamesettings.json file in your project folder (the same folder where main.lua is, which is the resource directory). This file will represent the default settings. In your code to load settings, you would first attempt to load a gamesettings.json file from the documents directory. If it doesn’t exist, then you would load the default gamesettings.json file from the resource directory (which you can then save to the documents directory).

  • Andrew

[import]uid: 109711 topic_id: 36846 reply_id: 145137[/import]

Hi Andrew,

thanks a lot for your quick answer. All file names are correct. When i started developing i heard this was a known issue on android devices, so i had an eye on that from the beginning.

The second possibility you mentioned sounds really interesting - i think the problem lies somewhere in that area. I thought, all files from the documents directory within the projects sandbox would be included into the apk file. Most of the articles i read about these i/o issues on android devices pointed out, that there is no resources directory on android devices anyway - so i haven´t thought about the possibility to use the resources directory for those files.

The method you described sounds like the right solution - i will try that.

Thank you very much for your help.

Alex
[import]uid: 196206 topic_id: 36846 reply_id: 145145[/import]

Was this resolved - I’m finding when I create a file using path = system.pathForFile (filename, system.DocumentsDirectory) the file is not saved/created, and later when I try to read the file I’m not able to find/read it?

I can’t find anything that suggests this shouldn’t work on Android, nor what permissions (if any) are needed. I wish Corona put more effort in the Android side of things to properly document the behaviours better… the cross platform stuff is brilliant but often let down when simple things like this aren’t consistent between platforms (or it isn’t documented to outline when it isn’t).

G

The only reason why I would think you could not read from system.DocumentsDirectory is because that folder starts empty.  You have to write files out to it before you can read from it.  There are Android issues trying to read some things from system.ResourcesDirectory (and of course you cannot write there). 

Was this resolved - I’m finding when I create a file using path = system.pathForFile (filename, system.DocumentsDirectory) the file is not saved/created, and later when I try to read the file I’m not able to find/read it?

I can’t find anything that suggests this shouldn’t work on Android, nor what permissions (if any) are needed. I wish Corona put more effort in the Android side of things to properly document the behaviours better… the cross platform stuff is brilliant but often let down when simple things like this aren’t consistent between platforms (or it isn’t documented to outline when it isn’t).

G

The only reason why I would think you could not read from system.DocumentsDirectory is because that folder starts empty.  You have to write files out to it before you can read from it.  There are Android issues trying to read some things from system.ResourcesDirectory (and of course you cannot write there).