Unable to read files from ResourceDirectory on Android

Hi there,

I have tried the example stated here (bottom of document):

http://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Added the required copyFile and doesFileExist functions as per examples and then called:

copyFile( “test.txt”, nil, “test.txt”, system.DocumentsDirectory, true )

Problem is, it silently (I have debugged on a Galaxy S3) won’t copy the file “test.txt” that sits in my project folder (where main.lua is)

Since the documentation says:

Here’s how to copy cat.png to the documents directory on Android, assuming it’s stored as cat.png.txt. This technique works for all platforms, so if you make it work for Android, it will work everywhere.

Is there any reason why this is not working?

PS. I have even added a permission “android.permission.WRITE_EXTERNAL_STORAGE” but it just won’t work.

This is the output in the logcat if I print system.pathForFile(nil, DocumentsDirectory) from the device:

07-02 21:19:40.849  17092-17118/? I/Corona﹕ /data/data/com.mycompany.myapp/app_data

Note: everything it works file in the simulator and the file gets copied.

After spending hours trying to make it work, I really hope I’m missing something here and the documentation is not incorrect.

Any ideas?

From the API docs:

Gotchas

To restate what was mentioned above: system.ResourceDirectory is where your main.lua file and generally all of your asset (resource) files are stored. For security reasons, this directory is read-only and enforced by the operating system, not by Corona.

 

This can be found here. Maybe I’m missing the point of your comment, but that’s the gist of the system.ResourceDirectory.

@Panc software what I’m saying is that I’m trying to copy a text file from the resource directory to the system.DocumentsDirectory as per example and explained here: http://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Not trying to write to the resources at all.

My fault, I misunderstood. 

I’m not an expert on I/O so I’ll defer to another CL dev.

Hi @lpalmieri,

Did you try simply reading the text file outright, as shown under “Reading Files” in the same guide? Text files are not one of the “restricted” types for Android, so I don’t think you need to bother with the copy/rename routines… however, I haven’t tested this in awhile so perhaps I’m incorrect. Can you please try to directly read the file and report back what happens?

http://docs.coronalabs.com/daily/guide/data/readWriteFiles/index.html#reading

Also, it probably doesn’t hurt to add “READ_EXTERNAL_STORAGE” as one of your permissions.

Thanks,

Brent

Hi.  Have you tried the GGFile module: https://github.com/GlitchGames/GGFile 

local GGFile = require( "GGFile" ) local fileManager = GGFile:new() fileManager:copy( "test.txt", system.ResourceDirectory, "test.txt", system.DocumentsDirectory ) local tmp = display.newText( "Text file in documents folder? ==\> " .. tostring(fileManager:exists( "test.txt", system.DocumentsDirectory)), 20, 200, native.systemFont, 30 ) tmp.anchorX = 0

Just tested on Gen1 Nexus 7 and the above works.

Hi @Brent
Yes, I can read and print the contents of the text file. The problem is when I try to write to the documents folder.

In that case, system.DocumentsDirectory doesn’t seem to return the device’s document folder but “/data/data/com.mycompany.myapp/app_data” (have now tested on a second device and got same result)

@roaminggamer thanks I will give it a try.

@lpalmieri,

Did you get this resolved?

@roaminggamer,

Thanks for this - I have GGFile and the code you’ve posted but it didn’t work.

Have also tried a new project with nothing else by GGFile and your code but no success either.

Printing the value of “system.DocumentsDirectory” on the device returns “/data/data/com.mycompany.myapp/app_data”

Shouldn’t that return “/Documents” or something similar instead?

Cheers,

Leandro

/data/data/com.mycompany.myapp/app_data is the sandbox of your app on device.  I would guess if you also printed out system.TemporaryDirectory you would get something like: /data/data/com.mycompany.myapp/app_data/tmp  and system.CachesDirectory should be the same ans system.TemporaryDirectory on Android.

Thanks @Rob,

So since “system.DocumentsDirectory” on the device is “/data/data/com.mycompany.myapp/app_data”, how do I get to the actual Documents folder in Android? (the folder that can be accessed by the user via file browser)

Thanks

You might be able to use the LFS module to get there.  Corona itself cannot reach outside of the Sandbox.

Rob

From the API docs:

Gotchas

To restate what was mentioned above: system.ResourceDirectory is where your main.lua file and generally all of your asset (resource) files are stored. For security reasons, this directory is read-only and enforced by the operating system, not by Corona.

 

This can be found here. Maybe I’m missing the point of your comment, but that’s the gist of the system.ResourceDirectory.

@Panc software what I’m saying is that I’m trying to copy a text file from the resource directory to the system.DocumentsDirectory as per example and explained here: http://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Not trying to write to the resources at all.

My fault, I misunderstood. 

I’m not an expert on I/O so I’ll defer to another CL dev.

Hi @lpalmieri,

Did you try simply reading the text file outright, as shown under “Reading Files” in the same guide? Text files are not one of the “restricted” types for Android, so I don’t think you need to bother with the copy/rename routines… however, I haven’t tested this in awhile so perhaps I’m incorrect. Can you please try to directly read the file and report back what happens?

http://docs.coronalabs.com/daily/guide/data/readWriteFiles/index.html#reading

Also, it probably doesn’t hurt to add “READ_EXTERNAL_STORAGE” as one of your permissions.

Thanks,

Brent

Hi.  Have you tried the GGFile module: https://github.com/GlitchGames/GGFile 

local GGFile = require( "GGFile" ) local fileManager = GGFile:new() fileManager:copy( "test.txt", system.ResourceDirectory, "test.txt", system.DocumentsDirectory ) local tmp = display.newText( "Text file in documents folder? ==\> " .. tostring(fileManager:exists( "test.txt", system.DocumentsDirectory)), 20, 200, native.systemFont, 30 ) tmp.anchorX = 0

Just tested on Gen1 Nexus 7 and the above works.

Hi @Brent
Yes, I can read and print the contents of the text file. The problem is when I try to write to the documents folder.

In that case, system.DocumentsDirectory doesn’t seem to return the device’s document folder but “/data/data/com.mycompany.myapp/app_data” (have now tested on a second device and got same result)

@roaminggamer thanks I will give it a try.

@lpalmieri,

Did you get this resolved?

@roaminggamer,

Thanks for this - I have GGFile and the code you’ve posted but it didn’t work.

Have also tried a new project with nothing else by GGFile and your code but no success either.

Printing the value of “system.DocumentsDirectory” on the device returns “/data/data/com.mycompany.myapp/app_data”

Shouldn’t that return “/Documents” or something similar instead?

Cheers,

Leandro