Using Flags and LocalFileSystem in PhoneGap


The LocalFileSystem object is used to supply arguments to DirectoryEntry objects such as the getFile and getDirectory methods, which you can use to look up or create files and directories, respectively.

The following are the properties.

  • create : This is used to specify that a file or directory should be created, if it does not exist.
  • exclusive : This is used with create. The exclusive flag causes the file or directory creation to fail if the target path already exists.

The following is an Example:

      // Get the data directory, creating it if it doesn't exist.
     dataDirect = fileSystem.root.getDirectory("data", {create: true});
    // Create the status file, if and only if it doesn't exist.
    statusFile = dataDir.getFile("statusfile.txt", { create: true, exclusive: true });

Using LocalFileSystem

This object provides a way to obtain root filesystems. It has two methods:

  • requestFileSystem : This method is used to request a fileSystem.

  • resolveLocalFileSystemURI : This method is used to retrieve a DirctoryEntry or FileEntry object using a local URI.

Two constants define a local filesystem. 

  • LocalFileSystem.PERSISTENT : This is used for the storage that is not deleted by any user agent without permission from the application or user.

  • LocalFileSystem.TEMPORARY : This is used for storage with no guarantee of persistence.

The following is an example that requests the local filesystem:

function onSuccess(fileSystem)
{
       console.log(fileSystem.name);
}
// request the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);

Here's an example that resolves the local system URI:

function onSuccess(fileEntry)
{
     console.log(fileEntry.name);
}
window.resolveLocalFileSystemURI("File:///new.txt", onSuccess, onError);

Full Example

<!DOCTYPE html>
<html>
  <head>
    <title>Local_File_System Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
    <script type="text/javascript" charset="utf-8">

        // Wait for PhoneGap to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // PhoneGap is ready
        //
        function onDeviceReady() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSuccess, fail);
            window.resolveLocalFileSystemURI("file://new.txt", onResolveSuccess,fail );
        }

        function onFileSuccess(fileSystem) {
            console.log(fileSystem.name);
        }

        function onResolveSuccess(fileEntry) {
            console.log(fileEntry.name);
        }

Resources

Some of the useful resources are as follows:

Working With Networks in Windows Phone 7 Using PhoneGap
Media in PhoneGap
Events in PhoneGap


Similar Articles