system.ResourceDirectory

Type Constant
Library system.*
Revision 2017.3060
Keywords system directory
See also system.pathForFile()
system.ApplicationSupportDirectory
system.CachesDirectory
system.DocumentsDirectory
system.TemporaryDirectory

Overview

Used with system.pathForFile() to create a path for retrieving files where all the application assets exist (for example, image and sound files). This is often called the “app bundle.”

You should not create, modify, or add files to this directory. Doing so will prevent the device from verifying the integrity of your application; in some cases the device will treat your application as malware and refuse to launch your application.

This property can also be used with other APIs requesting "baseDirectory" as a parameter (for example, display.newImage(), media.playSound(), etc.). The resource directory is generally the default directory if no directory is specified.

In the Corona Simulator this will be in a sandboxed folder on a per-application basis. Unlike the /Documents and /tmp directories, the resource directory is not viewable via File → Show Project Sandbox.

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.

Android

File access in Corona is based on the underlining operating system which varies by platform. On iOS devices, you can access files in all of the directories described above. On Android, however, there is no literal system.ResourceDirectory because all resource files reside inside a compressed .apk file.

Corona allows direct loading of images and audio files using the appropriate APIs, but it has limited access to resource files on Android using the file I/O APIs. Specifically, the following types can not be read from the resources directory: .html, .htm., .3gp, .m4v, .mp4, .png, .jpg, and .ttf.

Because of this limitation, if you have files of these types bundled in the core directory that you need to copy to another directory, you must change the file name so it can be accessed by the file I/O APIs. For example, if you want to move cat.png from the resource directory to the documents directory, it must be renamed cat.png.txt to be copied. See Working With Subfolders in the Reading and Writing Files guide for details.

Syntax

system.ResourceDirectory

Examples

This example will read a data.txt file located in the assets directory (the same directory containing main.lua).

local path = system.pathForFile( "data.txt", system.ResourceDirectory )
 
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