Doubts with Android and system.ResourceDirectory

Hi, I just start programming with Corona, and well, I’m blocked right now.

My first question is: 

in the ResourceDirectory, all files that exist (scripts, images, audio…) share the same directory? I.e. isn’t exist any sub directory? If so, why?

I’ve also been reading (correct me if my understanding is wrong) is not possible to directly read image files png or jpg from the ResourceDirectory, due to compression of the .apk format, claiming that it isn’t a “real directory”. Therefore, it is have to move to another directory, such as for example DocumentsDirectory. 

My second question comes to read that with certain API’s occurs, and with others not. Sincerely I don’t see nothing clear regarding this issue, and I need that you explain me all this. 

I have to tell you that the images that I have to enter my .apk are more than 600 different images, and I think that I’m stuck in a problem, or at least it has seemed to read everything related to directories in Android. 

I hope that you answer, and thank you very much for trying to help! 

Greetings, Dalvin

PD: Sorry for my strange English :slight_smile:

in the ResourceDirectory, all files that exist (scripts, images, audio…) share the same directory? I.e. isn’t exist any sub directory? If so, why?
system.ResourceDirectory allows sub folders. For instance, you can make a folder called images. Put your image in there and then in code do:

local background = display.newImageRect( "images/background.jpg", 570, 360 )

Some things do have to be in the same folder as main.lua such as the app’s Icons, config.lua, build.settings etc. You can even put additional lua files in a folder. For instance lets say you create a folder named modules and put your .lua files there (other than main.lua and config.lua). When you go to require them:

local myModule = require( "modules.mymodule" )

Notice it uses dot syntax instead of slash syntax.

On Android, (and this isn’t a Corona thing, its the way Android works), all of the files are compressed into a .zip file and renamed to .apk. It’s up to the app to unpack the files when needed. Corona SDK’s display.* API’s know how to extract the images from the APK. Any file opened with system.pathForFile() will also get extracted as part of that call. system.ResourceDirectory is a read-only file for multiple reasons. If you have a file you want to write too, such as a pre-loaded SQL database, you will have to copy that file to system.DocumentsDirectory before you can start updating it.

My second question comes to read that with certain API’s occurs, and with others not. Sincerely I don’t see nothing clear regarding this issue, and I need that you explain me all this.

I think I answered this above. But the exact explanation is here: https://docs.coronalabs.com/api/library/system/pathForFile.html

Rob

Thank you Rob, your answer resolved my doubts. But honestly, I don’t have any clear the issue of directories. Is 

there any guide with examples of use of directories in both S.O, android and iOS?

I still have doubts with the system.pathForFile (). Exactly I don’t know what it does, if creating a compatible directory with the Lua I/O and put there the file (although not exists), or return the full directory of a file for their management (because of the obscuration)… 

The only thing I know about this function is that it is important to know how it works, as well as the system directories. 

I wouldn’t know where I would have to use this function, that is the problem. I don’t know if its Corona which forces to use in certain cases, for example, when you create a database, or whenever you read or write a file… 

I’m somewhat confused, and again thanks Rob for answer! 

Regards, Dalvin.

We do have a guide for this:  https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Your app is locked into a “sandbox”. Apps can only read/write from directories that exist in this sandbox. We offer four:

system.ResourceDirectory – read only, where your physical app and assets exist.

system.DocumentsDirectory – read/write, store permanent things, like saved games, settings, databases, etc. This gets backed up automatically on iOS, so large files are not welcome here (unless you set them to not backup)

system.CachesDirectory – read/write, place to store things that can be re-downloaded from the Internet, large files, etc. On Android this is the same folder as…

system.TemporaryDirectory – place to put temporary files.

All path names in Corona are relative. For instance if you have a folder in your app called “data” and inside that folder you have a file called “data1.txt”. You need to be able to access that file. For things like display.newImage() you just have to specify “foldername/filename.ext” and it just works. But for data files, you have to get an actual path to the file that io.open() can use.

system.pathForFile() lets you take your relative path (“foldername/filename.ext”) and your sandbox directory (system.DocumentsDirectory) and it returns a usable path that io.open() uses.

Rob

in the ResourceDirectory, all files that exist (scripts, images, audio…) share the same directory? I.e. isn’t exist any sub directory? If so, why?
system.ResourceDirectory allows sub folders. For instance, you can make a folder called images. Put your image in there and then in code do:

local background = display.newImageRect( "images/background.jpg", 570, 360 )

Some things do have to be in the same folder as main.lua such as the app’s Icons, config.lua, build.settings etc. You can even put additional lua files in a folder. For instance lets say you create a folder named modules and put your .lua files there (other than main.lua and config.lua). When you go to require them:

local myModule = require( "modules.mymodule" )

Notice it uses dot syntax instead of slash syntax.

On Android, (and this isn’t a Corona thing, its the way Android works), all of the files are compressed into a .zip file and renamed to .apk. It’s up to the app to unpack the files when needed. Corona SDK’s display.* API’s know how to extract the images from the APK. Any file opened with system.pathForFile() will also get extracted as part of that call. system.ResourceDirectory is a read-only file for multiple reasons. If you have a file you want to write too, such as a pre-loaded SQL database, you will have to copy that file to system.DocumentsDirectory before you can start updating it.

My second question comes to read that with certain API’s occurs, and with others not. Sincerely I don’t see nothing clear regarding this issue, and I need that you explain me all this.

I think I answered this above. But the exact explanation is here: https://docs.coronalabs.com/api/library/system/pathForFile.html

Rob

Thank you Rob, your answer resolved my doubts. But honestly, I don’t have any clear the issue of directories. Is 

there any guide with examples of use of directories in both S.O, android and iOS?

I still have doubts with the system.pathForFile (). Exactly I don’t know what it does, if creating a compatible directory with the Lua I/O and put there the file (although not exists), or return the full directory of a file for their management (because of the obscuration)… 

The only thing I know about this function is that it is important to know how it works, as well as the system directories. 

I wouldn’t know where I would have to use this function, that is the problem. I don’t know if its Corona which forces to use in certain cases, for example, when you create a database, or whenever you read or write a file… 

I’m somewhat confused, and again thanks Rob for answer! 

Regards, Dalvin.

We do have a guide for this:  https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Your app is locked into a “sandbox”. Apps can only read/write from directories that exist in this sandbox. We offer four:

system.ResourceDirectory – read only, where your physical app and assets exist.

system.DocumentsDirectory – read/write, store permanent things, like saved games, settings, databases, etc. This gets backed up automatically on iOS, so large files are not welcome here (unless you set them to not backup)

system.CachesDirectory – read/write, place to store things that can be re-downloaded from the Internet, large files, etc. On Android this is the same folder as…

system.TemporaryDirectory – place to put temporary files.

All path names in Corona are relative. For instance if you have a folder in your app called “data” and inside that folder you have a file called “data1.txt”. You need to be able to access that file. For things like display.newImage() you just have to specify “foldername/filename.ext” and it just works. But for data files, you have to get an actual path to the file that io.open() can use.

system.pathForFile() lets you take your relative path (“foldername/filename.ext”) and your sandbox directory (system.DocumentsDirectory) and it returns a usable path that io.open() uses.

Rob