While playing with PhoneGap’s filesystem api I noticed that window.requestFileSystem() would only give me a path to the on-device file storage area (sdcard0) but not allow me to gain access to the external sd card (extSdCard).

[edit 11/29/2014] Please review this article: Browsing Filesystems in PhoneGap as it has details about the setup used to accomplish the subject of this article.

The following illustrates the above:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsFail);

function fsSuccess(fs){
  // the above prints this to the console:
  // {"name":"persistent",
  //  "root:{"isFile":false,
  //     "isDirectory":true,
  //     "name":"sdcard0",
  //     "fullPath":"file:///storage/sdcard0",
  //     "filesystem":null
  //  }
  // }

As you can see simply getting the filesystem returns the location at /storage/sdcard0 which is a couple of levels below the root. You would think that its not a big deal because you could use the getParent() method to go higher up the directory chain. In practice however getParent() on file:///storage/sdcard0 returns the same location.

What you can do then is change the value of the fileSystem’s “fullPath” property to the desired path and thus get the location one directory level above sdcard0 without using getParent(). This one edit can be used with a directoryReader() to reveal the external sd card.

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fail);

function fsSuccess(fs){
  if (fs.root.fullPath === 'file:///storage/sdcard0'){
    fs.root.fullPath = 'file:///storage'; // change the path
  // create directory reader
  var directoryReader = fs.root.createReader()
  // get a list of all entries in the directory

function dirSuccess(entries){
  // will print something like the following to the console
  // [{"isFile":false,"isDirectory":true,"name":"extSdCard",
  //    "fullPath":"file:///storage/extSdCard","filesystem":null},
  //  {"isFile":false,"isDirectory":true,"name":"sdcard0",
  //    "fullPath":"file:///storage/sdcard0","filesystem":null}
  // ]

And that’s all there is to it – from there you have an entry for the internal storage area and and the external sdcard storage to do with as your app requires.

By modifying the fullPath you can also get all the way to file:/// if you so desired though at that point you’d need to be careful what you enable your users to do.

As a side note I have noticed that the path to file://storage is reported as having a trailing slash contrary to other paths.