Samsung Electronics logo

Managing File Storages



Learning how to manage file storages is a basic data management skill:

  1. To list available storages, use the listStorages() method of the FileSystemManager interface to search for the storages available on the device.

    If the search is successful, a list of found FileSystemStorage objects is passed to the success event handler.

    /* Success event handler */
    function checkCorruptedRemovableDrives(storages) 
    { 
       for (var i = 0; i < storages.length; i++) 
       { 
          if (storages[i].type != "EXTERNAL")
             continue; 
          if (storages[i].state == "UNMOUNTABLE") 
             console.log("External drive " + storages[i].label + " is corrupted.");
       }
    }
    
    /* Search for the storages */
    webapis.filesystem.listStorages(checkCorruptedRemovableDrives);
  2. To get storage details based on the storage name (the label attribute), use the getStorage() method.

    The success callback receives the FileSystemStorage object containing the storage details as an input parameter.

    /* Success event handler */
    function onStorage(storage) 
    { 
       console.log("Storage found!" + storage.label);
    }
    
    /* Retrieve a storage */
    webapis.filesystem.getStorage("MMC", onStorage, onStorageError);
  3. To receive notifications on the storage state changes, for example, additions and removals, register an event handler with the addStorageStateChangeListener() method.

    An event is generated each time the storage state changes.

    var watchID;
    
    /* Define the event handler */ 
    function onStorageStateChanged(storage) 
    {
       if (storage.state == "MOUNTED")
       console.log("Storage " + storage.label + " was added!");
    }
    
    /* Register the event handler */
     watchID = webapis.filesystem.addStorageStateChangeListener(onStorageStateChanged);
  4. To stop receiving the notifications, use the removeStorageStateChangeListener() method:

    webapis.filesystem.removeStorageStateChangeListener(watchID);