system.pathForFile Returning Nil

Hi all,

I’ve read up on system.pathForFile on Android devices, but still have a question.

I’m storing a .RAD file (Radiance Particle Engine) in {root}/custom/particles folder.  Everything works fine in Simulator but system.pathForFile is returning nil on Android.

The code defining path is simple enough (dir = relative path, fx = filename):

function create(fx,dir) if dir==nil then path=system.pathForFile(fx) else path=system.pathForFile(dir.."\\"..fx) end .....

However, on Android device this will only work if I do NOT specify “dir” and store the .RAD files in the root of the project, which I really do not want to do.

I understand that the system.ResourcesDirectory constant is the default in system.pathForFile , which according to Documentation will return nil.  But as I said, if I don’t specify “dir”, this all works on my Android device!

My call to the method defining the path is like this:

create("file.rad", "custom/particles")

Any help would be appreciated.

Cheers.

Hi again.  Actually what was the question? 

UPDATE: Deleted my prior posts till I’m clear on question.

I read that again and it seems like you know it shouldn’t work and I just don’t quite get the question.  In fact, there is no question in the post.  :unsure:

A better format for a post is:

  1. What I did.  - This is essentially you telling us what you want to do.

  2. What I saw.

  3. What I expected to see and why I expected to see it. - This is where you are telling us why you think #2 is wrong.

  4. What I did to debug the issue.

  5. What can I do to get what I expected in #3?

Having not gotten that question  yet, “I am assuming you are having issues reading files on Android from sub-folders.”

Here is a test showing how to do it:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/androidResourceDirectory.zip

local function readFile( fileName ) local path = system.pathForFile( fileName, system.ResourceDirectory ) local f = io.open( path,"rb") if (f == nil) then print( "Failed to open", fileName ) return nil end fileContents = f:read( "\*a" ) io.close(f) return fileContents end local files = { "test1.txt", "test1.rad", "data/test1.txt", "data/test1.rad" } for i = 1, #files do print("Trying to read: ", files[i] ) local data = readFile( files[i] ) print( "Got:\n", data, "\n-----------------\n" ) end

Here is the output from logcat on my Android device:

I/Corona (21455): Trying to read: test1.txt I/Corona (21455): Got: I/Corona (21455): 1 I/Corona (21455): 2 I/Corona (21455): 3 I/Corona (21455): 4 I/Corona (21455): 5 I/Corona (21455): ----------------- I/Corona (21455): Trying to read: test1.rad I/Corona (21455): Got: I/Corona (21455): 1 I/Corona (21455): 2 I/Corona (21455): 3 I/Corona (21455): 4 I/Corona (21455): 5 I/Corona (21455): ----------------- I/Corona (21455): Trying to read: data/test1.txt I/Corona (21455): Got: I/Corona (21455): 1 I/Corona (21455): 2 I/Corona (21455): 3 I/Corona (21455): 4 I/Corona (21455): 5 I/Corona (21455): ----------------- I/Corona (21455): Trying to read: data/test1.rad I/Corona (21455): Got: I/Corona (21455): 1 I/Corona (21455): 2 I/Corona (21455): 3 I/Corona (21455): 4 I/Corona (21455): 5 I/Corona (21455): -----------------

That weird indent for first line is from print() adding a tab between args, not a error in the read.

pathForFile only works for pre-packaged files in the resource directory. ie, you can’t use it to create new files in the resource directory, you’ll need to create those elsewhere (fe documents directory)

Hey guys, sorry for the ambiguous (or completely absent) question…  The basis of my question was, “why this no work on device, but work in Simulator?”  

@roaming, luckily your deleted responses were also emailed to me as one of those contained the answer.  :)  The specific problem was caused by the back-slash when appending directory and filename.   New code is as follows:

if dir==nil then path=system.pathForFile(fx) else path=system.pathForFile(dir.."/"..fx) end

My other point of confusion was due to misreading of Corona Documentation.  I thought the docs were saying that system.ResourceDirectory was default, but would also perpetually return nil on Android.

Thanks for the help.  I’ll try to be more clear in future!

Cheers.

 

You simply can’t write to the resource directory/folder on devices (iOS or Android). 

This is a well documented and established rule.  The sandboxing and other permission rules for devices are very strict.

You can in Windows and OS X, but that is because:

  1. It is an actual folder.
  2. You have permission to do so.

Neither of those is true for Android.

Understood.  My issue dealt solely with reading a file that is present in the game files.

Hi again.  Actually what was the question? 

UPDATE: Deleted my prior posts till I’m clear on question.

I read that again and it seems like you know it shouldn’t work and I just don’t quite get the question.  In fact, there is no question in the post.  :unsure:

A better format for a post is:

  1. What I did.  - This is essentially you telling us what you want to do.

  2. What I saw.

  3. What I expected to see and why I expected to see it. - This is where you are telling us why you think #2 is wrong.

  4. What I did to debug the issue.

  5. What can I do to get what I expected in #3?

Having not gotten that question  yet, “I am assuming you are having issues reading files on Android from sub-folders.”

Here is a test showing how to do it:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/androidResourceDirectory.zip

local function readFile( fileName ) local path = system.pathForFile( fileName, system.ResourceDirectory ) local f = io.open( path,"rb") if (f == nil) then print( "Failed to open", fileName ) return nil end fileContents = f:read( "\*a" ) io.close(f) return fileContents end local files = { "test1.txt", "test1.rad", "data/test1.txt", "data/test1.rad" } for i = 1, #files do print("Trying to read: ", files[i] ) local data = readFile( files[i] ) print( "Got:\n", data, "\n-----------------\n" ) end

Here is the output from logcat on my Android device:

I/Corona (21455): Trying to read: test1.txt I/Corona (21455): Got: I/Corona (21455): 1 I/Corona (21455): 2 I/Corona (21455): 3 I/Corona (21455): 4 I/Corona (21455): 5 I/Corona (21455): ----------------- I/Corona (21455): Trying to read: test1.rad I/Corona (21455): Got: I/Corona (21455): 1 I/Corona (21455): 2 I/Corona (21455): 3 I/Corona (21455): 4 I/Corona (21455): 5 I/Corona (21455): ----------------- I/Corona (21455): Trying to read: data/test1.txt I/Corona (21455): Got: I/Corona (21455): 1 I/Corona (21455): 2 I/Corona (21455): 3 I/Corona (21455): 4 I/Corona (21455): 5 I/Corona (21455): ----------------- I/Corona (21455): Trying to read: data/test1.rad I/Corona (21455): Got: I/Corona (21455): 1 I/Corona (21455): 2 I/Corona (21455): 3 I/Corona (21455): 4 I/Corona (21455): 5 I/Corona (21455): -----------------

That weird indent for first line is from print() adding a tab between args, not a error in the read.

pathForFile only works for pre-packaged files in the resource directory. ie, you can’t use it to create new files in the resource directory, you’ll need to create those elsewhere (fe documents directory)

Hey guys, sorry for the ambiguous (or completely absent) question…  The basis of my question was, “why this no work on device, but work in Simulator?”  

@roaming, luckily your deleted responses were also emailed to me as one of those contained the answer.  :)  The specific problem was caused by the back-slash when appending directory and filename.   New code is as follows:

if dir==nil then path=system.pathForFile(fx) else path=system.pathForFile(dir.."/"..fx) end

My other point of confusion was due to misreading of Corona Documentation.  I thought the docs were saying that system.ResourceDirectory was default, but would also perpetually return nil on Android.

Thanks for the help.  I’ll try to be more clear in future!

Cheers.

 

You simply can’t write to the resource directory/folder on devices (iOS or Android). 

This is a well documented and established rule.  The sandboxing and other permission rules for devices are very strict.

You can in Windows and OS X, but that is because:

  1. It is an actual folder.
  2. You have permission to do so.

Neither of those is true for Android.

Understood.  My issue dealt solely with reading a file that is present in the game files.