Skip to content

File

Non-Visible component
Category Requires Version
Storage API 21, Android 5.0 Lollipop 8

Overview

Non-visible component for storing and retrieving files. Use this component to write or read files on your device. The default behaviour is to write files to the private data directory associated with your App. The Companion is special cased to write files to a public directory for debugging. Use the More information link to read more about how the File component uses paths and scopes to manage access to files.

Events

After File Saved

Event indicating that the contents of the file have been written.

Params
file Name Text

After Unzip

Event indicating that the zip file have been created.

After Zip

Event indicating that the zip file have been created.

Directory Created

⚠ Deprecated

Event indicating that there was a directory created. The return value is 'true' or 'false'.

Params
is Directory Created Boolean

Got Text

Event indicating that the contents from the file have been read.

Params
text Text

On Zip Failure

Event indicating that there was any failure on zip or unzip a file.

Params
error Message Text

Methods

Append To File

Appends text to the end of a file storage, creating the file if it does not exist. See the help text under SaveFile for information about where files are written.

Params
text Text
file Name Text

Copy

⚠ Deprecated

Copy a file. If input path started with two // (slashes) then it's a asset file. You can not copy a file into the assets directory. Use CopyFile instead.

Params
input Text
output Text

Copy File

Returns: Boolean

Copy the contents from the first file to the second file.

Params
from Scope Com.google.appinventor.components.common.filescopeenum
from File Name Text
to Scope Com.google.appinventor.components.common.filescopeenum
to File Name Text

Create Directory

⚠ Deprecated

Create a new directory. Use MakeDirectory instead.

Params
directory Text

Delete

Deletes a file from storage. Prefix the filename with / to delete a specific file in the SD card, for instance /myFile.txt. will delete the file /sdcard/myFile.txt. If the file does not begin with a /, then the file located in the programs private storage will be deleted. Starting the file with // is an error because assets files cannot be deleted.

Params
file Name Text

Exists

Returns: Boolean

Tests whether the path exists in the given scope.

Params
scope Com.google.appinventor.components.common.filescopeenum
path Text

File Size

Returns: Number

Get file size

Params
file Text

Get File Name

Returns: Text

Get file name

Params
file Text

Get Free Space

Returns: Number

Get Free Space

Params
path Text

Get Total Space

Returns: Number

Get total space

Params
path Text

Is Directory

Returns: Boolean

Tests whether the path named in the given scope is a directory.

Params
scope Com.google.appinventor.components.common.filescopeenum
path Text

Is File

Returns: Boolean

Check whether the path is a file

Params
path Text

List Directory

Returns: List

Get a list of files and directories in the given directory.

Params
scope Com.google.appinventor.components.common.filescopeenum
directory Name Text

Make Directory

Returns: Boolean

Create a new directory for storing files. The semantics of this method are such that it willreturn true if the directory exists at its completion. This can mean that the directory alreadyexisted prior to the call.

Params
scope Com.google.appinventor.components.common.filescopeenum
directory Name Text

Make Full Path

Returns: Text

Converts the scope and path into a single string for other components.

Params
scope Com.google.appinventor.components.common.filescopeenum
path Text

Move

⚠ Deprecated

Returns: Boolean

Move a file. You can not move asset files. Use MoveFile instead.

Params
input Text
output Text

Move File

Returns: Boolean

Move a file from one location to another.

Params
from Scope Com.google.appinventor.components.common.filescopeenum
from File Name Text
to Scope Com.google.appinventor.components.common.filescopeenum
to File Name Text

Read From

Reads text from a file in storage. Prefix the filename with / to read from a specific file on the SD card. for instance /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 applications private storage (for packaged apps) and from /sdcard/AppInventor/data for the Companion.

Params
file Name Text

Remove Directory

Returns: Boolean

Remove a directory from the file system. If recursive is true, then everything is removed. Ifrecursive is false, only the directory is removed and only if it is empty.

Params
scope Com.google.appinventor.components.common.filescopeenum
directory Name Text
recursive Boolean

Save File

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 programs 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 a file use the append block.

Params
text Text
file Name Text

Unzip

Unzip a file with or without a password. If you dont need a passwort then let it empty.

Params
target Zip File Path Text
destination Folder Path Text
password Text

Zip

Create a zip file with or without a password.

Params
target Path Text
destination File Path Text
password Text

Properties

Default Scope

Com.google.appinventor.components.common.filescopeenum Default: App ➖ Write - Designer

Specifies the default scope for files accessed using the File component. The App scope shouldwork for most apps. Legacy mode can be used for apps that predate the newer constraints inAndroid on app file access.

Read Permission

Boolean Default: False ➖ Write - Designer

A designer-only property that can be used to enable read access to file storage outside of theapp-specific directories.

Scope

Com.google.appinventor.components.common.filescopeenum ➖ Read Write - Blocks

Indicates the current scope for operations such as ReadFrom and SaveFile.

Write Permission

Boolean Default: False ➖ Write - Designer

A designer-only property that can be used to enable write access to file storage outside of theapp-specific directories.


Last update: November 9, 2022