Files and folders

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 FileFolder 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 FileFolder 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.

FileFolder { //Instantiates FileFolder object
        id: fileFolder
    }

    Button {

        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        text:"Create Folder"
        onClicked: {
            fileFolder.path= "~/ArcGIS/Example App" //Directs FileFolder towards a location within named user directory
            var newFileFolder = fileFolder.folder("Bruce"); //Creates a folder object by the name Bruce
            var 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 StandardPaths 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 StandardPaths component' standardLocations enum to inform the AppFramework resolvedPathUrl function.

FileDialog {
            title: qsTr("Open Photo")

            folder: Qt.platform.os == "ios"
            ? "file:assets-library://"
            : AppFramework.resolvedPathUrl(AppFramework.standardPaths.standardLocations(StandardPaths.PicturesLocation)[0])

            onAccepted: {
            imageObject.load(fileUrl);
                    }
        }

StandardPaths 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 FileFolder 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, FileFolder 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 FileInfo component, used to return information about a file, is used in this case to reliably locate the text file.

FileInfo {
            id: fileInfo
            filePath: fileFolder.path + "/TestFile.txt" //Instantiates FileInfo to locate the file
        }

        File {
            id:file
            path:fileInfo.filePath //FileInfo's filePath is passed to File component
        }

        TextField {
            id:editingField
            onEditingFinished: { //Opens text file, writes to it and closes it once user has finished writing to the field
                file.open(File.OpenModeReadWrite)
                file.write(editingField.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.