9

In PhoneGap, I use

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

to access the file system.
In my ASUS tablet, it has no external sdcard(I don't insert any removable device) so I think the file system root points to the internal sdcard. However, in my HTC Desire HD, the data was written to the external sdcard. (Since the data just reside in the microSD card.)
So what is the truth? I can't see any clues in the W3C document, maybe I miss something...

PS: Both the android version are ICS(Ice cream sandwich).

1 Answer 1

16

PhoneGap's FileAPI, while designed to mirror the HTML5 spec, is actually a custom implementation of the W3C document. You can find the docs specific to their API here. While it mostly can be used the same, there are some subtle differences between how things are implemented on the web and per device. The location of storage is one of these.

To find out how PhoneGap handles persistent storage, I had to dig into the Cordova source code. This file here contains the methods used by the PhoneGap FileAPI. The relevant block of code starts at line 871. Basically, the API will make a call to Environment.getExternalStorageState(). If this returns Environment.MEDIA_MOUNTED, meaning there's either an removable or non-removable SD card for storage, the FileSystem returned by the API is the root directory of the mounted storage, using Environment.getExternalStorageDirectory(). This explains the difference in behavior you saw between devices with internal and external SD cards, both considered mounted external storage by the system. If you encounter a device without any external storage, i.e. !Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED), the root of the returned FileSystem will be "data/data/packageName" in internal storage, similar to calling Context.getFilesDir(), which usually returns something like "data/data/packageName/files".

9
  • 1
    You are unbelievable great! But I have a question: does line 868 fs.put("root", getEntry("/data/data/" + cordova.getActivity().getPackageName() + "/cache/")); mean the system create a directory called /data/data/ +~ +/cache/ ? It creates a space for user as a storage space just like if the user is on the web instead of using mobile device?
    – Alston
    May 6, 2013 at 15:47
  • Btw: How do you find the source code?... It's too complex to know which file is responsible for this functionality...
    – Alston
    May 6, 2013 at 15:51
  • 2
    Yes. That line in particular is for Temporary storage, so they created the cache directory to denote that. For any Android app, PhoneGap or native, the "/data/data/packageName" is created as a private space for that App's files. PhoneGap is just utilizing that directory in this case.
    – MattDavis
    May 6, 2013 at 15:53
  • 1
    You can find the source code for each platform's PhoneGap implementation if you scroll down a bit here cordova.apache.org/#contribute There's not really a guide to it that I've found, but if you check out docs.phonegap.com/en/2.7.0/index.html you can see the different APIs provided by PhoneGap and try to pair the files with their functionality.
    – MattDavis
    May 6, 2013 at 15:56
  • 1
    So. The internal storage actually points to "/data/data" and any additional sdcard are called external storage no matter it is removable or not (installed in the machine and we can't see it) ? The name of sdcard is misleading...
    – Alston
    May 6, 2013 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.