pathforfile android

Hi all,

using the following code from the Corona Reference:

local path = system.pathForFile( “data.txt”, system.DocumentsDirectory )
local file = io.open( path, “r” )
if file then – nil if no file found
local contents = file:read( “*a” )
print( "Contents of " … path … “\n” … contents )
io.close( file )
end

I can load in and display text fine in simulator, but when I build and open the program on my android device nothing happens. In fact when I add an else statement to display some generic text it won’t even display that (so I guess the program is breaking). I’ve checked case sensitivity etc. my text file is really basic, just has the words “Hello android” in it (without the quotation marks) and is located in the root folder where the main.lua file exists. Is there something I’m missing? is this an android limitation?

Marty [import]uid: 7548 topic_id: 1494 reply_id: 301494[/import]

– an edit to the above post, the local path line i’m using is

local path = system.pathForFile( “stitch.txt”, system.ResourceDirectory ) [import]uid: 7548 topic_id: 1494 reply_id: 4186[/import]

This is case #381 [import]uid: 7559 topic_id: 1494 reply_id: 4187[/import]

Sorry for being a n00b. I’ve looked everywhere and can’t seem to find where to look up the case number you’ve provided me with.

Marty [import]uid: 7548 topic_id: 1494 reply_id: 4188[/import]

Unfortunately there’s no place to view bug reports as of now :confused: [import]uid: 6678 topic_id: 1494 reply_id: 4189[/import]

The case numbers are the way we track bugs/features internally. We hope to make that list public soon.
When we do SDK releases, we try to list the bug/features numbers that were resolved in that build.

-Tom [import]uid: 7559 topic_id: 1494 reply_id: 4190[/import]

No problems, thanks for the quick replies!

Marty [import]uid: 7548 topic_id: 1494 reply_id: 4191[/import]

I’m running into the same problem even when using Beta8. But issue #381 is also not listed as resolved in the bug fixes list.

Tom, could you please elaborate a bit more what the case #381 is exactly about as we have no access to your internal bug reporting system? From my testing on the Android emulator it appears that system.pathForFile (system.ResourceDirectory) returns the filename unmodified and io.open cannot find the file. Is the problem that io.open cannot load resources in the package assets of an Android application or should system.pathForFile return a different path? The strange thing though is that images can be loaded correctly from the package assets (e.g. display.newImage(“test.png”)).

Thomas [import]uid: 8111 topic_id: 1494 reply_id: 5465[/import]

I’m having the same problem with pathForFile on Android as the original poster describes. So I’m interested if there is any workaround possible right now. Any update or further information on issue #381? [import]uid: 8460 topic_id: 1494 reply_id: 5544[/import]

The bug hasn’t been resolved yet but is scheduled for being fixed for the next release. Our Android developer is on vacation so I don’t have an update on the issue. Our internal database has a copy of the original posting along with the example code.

One thing we found related to sound files on the Android is that the file extensions (e.g., .mp3, .ogg, .txt) are stripped off when packaged into an apk file. Have you tried doing the file search without the .txt extension?

-tom [import]uid: 7559 topic_id: 1494 reply_id: 5647[/import]

Hi Tom

I tried the file search without extension, but that didn’t help. I had a look inside the package (APK file) and the file extensions are not stripped off. So let’s see when your Android developer is back.

Thomas [import]uid: 8111 topic_id: 1494 reply_id: 5650[/import]

I talked to the Android developer and here’s the story. Android doesn’t has a resource directory that can be accessed using file open. The assets packaged in the apk file (images and sound files) are located in the apk file which is really just a zip file. The APIs that use these assets (newImage and playSound) use internal Android APIs to access the files in the apk file. This means system.resourceDirectory will not work for Android in the current release.

The plan going forward (hopefully for the Corona SDK release) is to create a temporary Resource directory that is created when system.pathForFile() is called with the baseDirectory set to the resource directory. The specified file will be copied from the apk file into this temporary directory which will allow access using the file IO calls.

Currently the only work-around for this problem is to download any assets needed from an external server/website into the documents or temporary directory. We know it’s not an ideal solution which is why we are looking to fix the problem as soon as possible.

-Tom [import]uid: 7559 topic_id: 1494 reply_id: 5696[/import]

This is fixed in today’s release. [import]uid: 54 topic_id: 1494 reply_id: 6067[/import]

Eric, todays release notes point to the documentation which mentions (this morning) that you should download extra content from a website. Does your post means we don’t need to anymore? [import]uid: 5712 topic_id: 1494 reply_id: 6167[/import]

I have a question since I don’t understand what was made possible with the latest SDK release (I read documentation).

Is it possible (if yes how) to save information into a txt file (or any other maybe?). Then load that information on the next run?

Thank you!

Kreso [import]uid: 4294 topic_id: 1494 reply_id: 6179[/import]

@kreso,

Yes, you can save and load files from the Documents directory (system.DocumentsDirectory). The system.ResourceDirectory is read-only and used to access files (assets) that were packaged with your application.

Here are a couple of links that show how to save and load data:
http://developer.anscamobile.com/code/save-and-restore-state
http://developer.anscamobile.com/forum/2010/09/10/need-some-pointers-save-date-display-table-etc

-Tom [import]uid: 7559 topic_id: 1494 reply_id: 6183[/import]

Just to let other readers of this post know, the workaround in the new Corona SDK v2.0 appears to be as follows:

If a file path is requested on Android with system.pathForFile from the system.ResourceDirectory the file is copied to the following path:

/data/data/[package name]/files/coronaResources/

Thus a subsequent call to io.open will actually read the file from the application data folder instead of the application package.

I would assume this is only necessary for writeable files, but read-only files could still be read from the package directly? Is there any plan to make a difference between files to read or write?

I think you should update your official documentation, as this might be interesting for all Android developers who don’t stumble on this forum entry. [import]uid: 8111 topic_id: 1494 reply_id: 6296[/import]

This is documented in the release notes:
http://developer.anscamobile.com/downloads/CoronaSDK

This is necessary for readable files, and you shouldn’t attempt to write to files in system.ResourceDirectory (that will fail on iOS).

Our process is to make a release then gradually update the reference:
http://developer.anscamobile.com/reference/
This usually takes a few days.
[import]uid: 54 topic_id: 1494 reply_id: 6331[/import]

@MikeHart, you can download data, or you can include it in the APK and retrieve using PathForFile, your choice. Be aware that either method could fail if the device is out of space. [import]uid: 54 topic_id: 1494 reply_id: 6332[/import]