Samsung Web API enables you to manage the files and directories in the device file system using the Filesystem API.
The Filesystem API provides access to accessible parts of the file system, which are represented as virtual root locations. The virtual roots form a collection of locations that function as a single virtual device file system. The following table lists the supported virtual roots.
|documents||Location for storing documents.|
|downloads||Location for storing downloaded items.|
|images||Location for storing images.|
|music||Location for storing audio files.|
|removable||Location for the removable storage.|
|videos||Location for storing videos.|
|wgt-package||Location for storing widget packages (read-only location).|
|wgt-private||Location for the widget private storage.|
|wgt-private-tmp||Location for the widget private temporary storage.|
The main features of the Filesystem API include:
- File storage management
You can retrieve additional information about the storages, including listing available storages and receiving storage change notifications with the listStorages() and addStorageStateChangeListener() methods provided by the FileSystemStorage interface.
- Access to files and directories
You can access the virtual file system using the FileSystemManager interface.
To access a file or directory within the virtual file system, you must use the fully qualified path, <root name>/<path>, where, <rootname> is the name of the virtual root and <path> is the relative path to the file or directory within the root.
Note When you use a path to access the device file system, make sure that the file path encoding uses the default encoding of the platform.
To access a file or directory, you must also retrieve a file handle using the resolve() method of the FileSystemManager interface. A file handle is a reference object that points to and represents a file or directory.
The isFile and isDirectory attributes of the File interface identify the type of the object: for example, for a file, the isFile attribute is set to true and the isDirectory attribute to false.
- Files and directory management
You can perform basic file and directory management tasks using the File interface:
Create files and directories using the createFile() and createDirectory() methods.
The file or directory is created relative to the current directory that the operation is performed on.
Note Do not use "." or ".." characters in the directory or file path components.
- Retrieve a list of files or file URIs using the listFiles() and toURI() methods. The URI can be used to identify the file, for example, by using it as the src attribute on an HTML img element.
You can retrieve file content as a DOMString with the readAsText() method. The encoding input parameter of the method defines the format in which the file content is returned.
- Read or write to a file by first using the openStream() method to open the file. You can specify the file mode and encoding.
The openStream() method returns a FileStream object, which is a handle to the opened file. All actual operations, such as read, write, or close, on the file are performed through the FileStream object based on a position attribute, which represents the current position in the file.
- Copy and move files and directories within the virtual file system with the copyTo() and moveTo() methods.
If a file or directory of the same name already exists in the target location, the overwrite input parameter of the method defines whether the existing file is overwritten.
Note The file or directory to be copied or moved must be located under the current directory.
Delete files and directories from the virtual file system using the deleteFile() and deleteDirectory() methods.