system.pathForFile on Android Returns Path when File Does Not Exist

I’m having an issue with the system.pathForFile call on Android. I’m testing whether certain files, specifically MP3 files, exist in a subfolder.

If the file does not exist, then the first time this call is made, it correctly returns nil.

However, all subsequent calls, even on subsequent launches of the app , incorrectly return a path that does not exist.

The path returned is:

/data/data/PACKAGENAME/files/coronaResources/sounds/FILENAME.mp3

(with PACKAGENAME being my unique identifier, and FILENAME the file I am testing)

This would indicate that maybe Corona is copying/setting/creating something the first time the call is made, which then persists forever.

I know there are limitations using this call on Android, but nothing in the documentation mentions MP3 files.

Help!

Update: Also tried using the base (resources) directory with the same result, so it does not appear to be an issue with subfolders.

Hi @jmaher,

How are you testing if the file exists? Can I see your code? Have you tried using LFS (LuaFileSystem) for this?

Brent

Brent, it’s really simple:

local path = system.pathForFile(FILENAME.mp3) print("path",path) if path == nil then -- else -- end

This is in the create function of a composer scene. Works fine on iOS, and properly on Android the very first time.

Thanks,

Jeremy

Hi Jeremy,

So you’re not putting FILENAME.mp3 in quotes? If not, why? Also, did you follow the example in the docs which uses io.open() afterward?

http://docs.coronalabs.com/api/library/system/pathForFile.html

Take care,

Brent

Brent,

The actual FILENAME is concatenated from a series of variables, which I omitted for clarity. The path is then passed to an audio module, where audio.loadSound is called.

But as I said, before all that, just using the print statement I can immediately tell that on Android, all calls after the very first, even if the app is completely stopped and restarted, return an (incorrect) path rather than nil.

To me, this would indicate some fundamental issue in the way that system.pathForFile works on Android.

Thanks for your help,

Jeremy

Hi Jeremy,

Yes, there are some definite differences because of the way files are packaged inside the APK. It may help if I can see the exact string you’re trying to use… not the entire chain that you concatenate, but the exact result before you use it.

Thanks,

Brent

Hi @jmaher,

How are you testing if the file exists? Can I see your code? Have you tried using LFS (LuaFileSystem) for this?

Brent

Brent, it’s really simple:

local path = system.pathForFile(FILENAME.mp3) print("path",path) if path == nil then -- else -- end

This is in the create function of a composer scene. Works fine on iOS, and properly on Android the very first time.

Thanks,

Jeremy

Hi Jeremy,

So you’re not putting FILENAME.mp3 in quotes? If not, why? Also, did you follow the example in the docs which uses io.open() afterward?

http://docs.coronalabs.com/api/library/system/pathForFile.html

Take care,

Brent

Brent,

The actual FILENAME is concatenated from a series of variables, which I omitted for clarity. The path is then passed to an audio module, where audio.loadSound is called.

But as I said, before all that, just using the print statement I can immediately tell that on Android, all calls after the very first, even if the app is completely stopped and restarted, return an (incorrect) path rather than nil.

To me, this would indicate some fundamental issue in the way that system.pathForFile works on Android.

Thanks for your help,

Jeremy

Hi Jeremy,

Yes, there are some definite differences because of the way files are packaged inside the APK. It may help if I can see the exact string you’re trying to use… not the entire chain that you concatenate, but the exact result before you use it.

Thanks,

Brent