File Component


Properties

defaultScope
  Specifies the default scope for files accessed using the File component. The App scope should work for most apps. Legacy mode can be used for apps that predate the newer constraints in Android on app file access.
Data type: string
Designer Writable true <file name="fileName" defaultScope="App">
Code Writeable false
Code Readable false

readPermission
  A designer-only property that can be used to enable read access to file storage outside of the app-specific directories.
Data type: boolean
Designer Writable true <file name="fileName" readPermission="true">
Code Writeable false
Code Readable false

scope
  Indicates the current scope for operations such as ReadFrom and SaveFile.
Data type: string
Designer Writable false
Code Writeable true fileName.scope = "App"
Code Readable true let variable = fileName.scope

writePermission
  A designer-only property that can be used to enable write access to file storage outside of the app-specific directories.
Data type: boolean
Designer Writable true <file name="fileName" writePermission="true">
Code Writeable false
Code Readable false

class
  The styling class of the the component
Data type: string
Designer Writable true <file name="fileName" class="Test class">
Code Writeable false
Code Readable false

id
  The styling id of the the component
Data type: string
Designer Writable true <file name="fileName" id="Test id">
Code Writeable false
Code Readable false

name
  The name of the component that will be used to refer to it in code.
Data type: string
Designer Writable true <file name="fileName" name="testComponent">
Code Writeable false
Code Readable false

Methods

Method name Description Parameters
appendToFile Appends text to the end of a file. Creates the file if it does not already exist. See the help text under SaveFile for information about where files are written. On success, the AfterFileSaved event will run.
fileName.appendToFile(text, fileName)
fileName.appendToFile("Test text", "Test fileName")
text string
fileName string
copyFile Copy the contents from the first file to the second file.
fileName.copyFile(fromScope, fromFileName, toScope, toFileName)
fileName.copyFile("App", "Test fromFileName", "App", "Test toFileName")
fromScope com.google.appinventor.components.common.FileScopeEnum
fromFileName string
toScope com.google.appinventor.components.common.FileScopeEnum
toFileName string
delete Deletes a file from storage. Prefix the fileName with / to delete a specific file in the SD card (for example, /myFile.txt will delete the file /sdcard/myFile.txt). If the fileName does not begin with a /, then the file located in the program's private storage will be deleted. Starting the fileName with // is an error because asset files cannot be deleted.
fileName.delete(fileName)
fileName.delete("Test fileName")
fileName string
exists Tests whether the path exists in the given scope.
fileName.exists(scope, path)
fileName.exists("App", "Test path")
scope com.google.appinventor.components.common.FileScopeEnum
path string
isDirectory Tests whether the path named in the given scope is a directory.
fileName.isDirectory(scope, path)
fileName.isDirectory("App", "Test path")
scope com.google.appinventor.components.common.FileScopeEnum
path string
listDirectory Get a list of files and directories in the given directory.
fileName.listDirectory(scope, directoryName)
fileName.listDirectory("App", "Test directoryName")
scope com.google.appinventor.components.common.FileScopeEnum
directoryName string
makeDirectory Create a new directory for storing files. The semantics of this method are such that it will return true if the directory exists at its completion. This can mean that the directory already existed prior to the call.
fileName.makeDirectory(scope, directoryName)
fileName.makeDirectory("App", "Test directoryName")
scope com.google.appinventor.components.common.FileScopeEnum
directoryName string
makeFullPath Converts the scope and path into a single string for other components.
fileName.makeFullPath(scope, path)
fileName.makeFullPath("App", "Test path")
scope com.google.appinventor.components.common.FileScopeEnum
path string
moveFile Move a file from one location to another.
fileName.moveFile(fromScope, fromFileName, toScope, toFileName)
fileName.moveFile("App", "Test fromFileName", "App", "Test toFileName")
fromScope com.google.appinventor.components.common.FileScopeEnum
fromFileName string
toScope com.google.appinventor.components.common.FileScopeEnum
toFileName string
readFrom Reads text from a file in storage. Prefix the fileName with / to read from a specific file on the SD card (for example, /myFile.txt will read the file /sdcard/myFile.txt). To read assets packaged with an application (also works for the Companion) start the fileName with // (two slashes). If a fileName does not start with a slash, it will be read from the application's private storage (for packaged apps) and from /sdcard/AppInventor/data for the Companion.
fileName.readFrom(fileName)
fileName.readFrom("Test fileName")
fileName string
removeDirectory Remove a directory from the file system. If recursive is true, then everything is removed. If recursive is false, only the directory is removed and only if it is empty.
fileName.removeDirectory(scope, directoryName, recursive)
fileName.removeDirectory("App", "Test directoryName", true)
scope com.google.appinventor.components.common.FileScopeEnum
directoryName string
recursive boolean
saveFile Saves text to a file. If the fileName begins with a slash (/) the file is written to the sdcard (for example, writing to /myFile.txt will write the file to /sdcard/myFile.txt). If the fileName does not start with a slash, it will be written in the program's private data directory where it will not be accessible to other programs on the phone. There is a special exception for the AI Companion where these files are written to /sdcard/AppInventor/data to facilitate debugging. Note that this block will overwrite a file if it already exists. If you want to add content to an existing file use the AppendToFile method.
fileName.saveFile(text, fileName)
fileName.saveFile("Test text", "Test fileName")
text string
fileName string
addEventListener Method used to create event listeners.
See Events below for samples.
eventName string
eventCallbackFunction callback

Events

Event name Description Parameters
afterFileSaved Event indicating that the contents of the file have been written.
fileName.addEventListener(
    "afterFileSaved",
    function (fileName) {
        //Your code here
    }
)
fileName string
gotText Event indicating that the contents from the file have been read.
fileName.addEventListener(
    "gotText",
    function (text) {
        //Your code here
    }
)
text string