While much of your app's behavior and operations can be performed within the app itself, including some limited storage capabilities, most apps require some degree of access to files on the device. Creating and writing to files, selecting already saved photos, and downloading files from an online source all require some degree of file or folder management. The AppStudio Framework has multiple components intended to create, organize, and navigate folders and files.
Folder management
The AppFramework File
component provides tools to manipulate, navigate, create, and delete folders and file paths. If your app is creating or downloading new files of any type, using File
can be important for keeping the internal file structure of your app orderly and consistent.
This code sample creates a new folder in a set location, which is ideal if your app will be creating a large number of files that need to be grouped together.
let fileFolder = AppFramework.fileFolder("~/ArcGIS/Example App"); //Directs FileFolder towards a location within named user directory
let newFileFolder = fileFolder.folder("Bruce"); //Creates a folder object by the name Bruce
let result = newFileFolder.makeFolder(); //Creates folder at the location described by fileFolder.path
Standard file paths
Operating systems have specific standard locations to save certain types of file. The Standard
component provides a means to use these locations as a default folder path.
This code sample opens a file dialog specifically to the default location for pictures, using the Standard
component' standard
enum to inform the AppFramework resolved
function.
FileDialog {
title: qsTr("Open Photo")
folder: Qt.platform.os == "ios"
? "file:assets-library://"
: AppFramework.standardPaths.defaultFolder(StandardPaths.PicturesLocation).url
onAccepted: {
imageObject.load(fileUrl);
}
}
Standard
also has methods to determine if a folder or location can be written to, which is useful for configuring an app to write to an appropriate location.
Create and write to a file
In addition to creating and navigating folders, the File
component can also be used for creating simple text and JSON files. Adding the following line to the onClicked events in the folder creation sample above will create a text file within the folder:
fileFolder.writeTextFile("TestFile.txt", "Contents of the file")
However, File
can only create the file initially and cannot read or write to it after creation. To do this, the File
component must be used; a component specifically used to work with text files in this way.
This code sample presents a text field that can be used to write to the text file created previously. The File
component, used to return information about a file, is used in this case to reliably locate the text file.
FileInfo {
id: fileInfo
filePath: "~/ArcGIS/Apps/SampleApp/TestFile.txt" //Instantiates FileInfo to locate the file
}
function saveText(text) {
let fileFolder = fileInfo.folder;
fileFolder.makeFolder();
let file = AppFramework.file(fileInfo.filePath);
file.open(File.OpenModeReadWrite)
file.write(text);
file.close();
}
Keep in mind that the File
component is a plain text editor. This means that formatted text, as well as non-text content, will not read or display correctly. They can still be written to the file, however.