Where does LocalFileSystem.PERSISTENT point to?

10,563

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".

Share:
10,563

Related videos on Youtube

Alston
Author by

Alston

A lover of Java, Android and Python. :)

Updated on September 16, 2022

Comments

  • Alston
    Alston over 1 year

    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).

  • Alston
    Alston almost 11 years
    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
    Alston almost 11 years
    Btw: How do you find the source code?... It's too complex to know which file is responsible for this functionality...
  • MattDavis
    MattDavis almost 11 years
    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
    MattDavis almost 11 years
    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.
  • Alston
    Alston almost 11 years
    But wait...Uh... You said: meaning there's either an removable or non-removable SD card for storage. I think what you want to talk is if Environment.getExternalStorageState().equals(Environment.MED‌​IA_MOUNTED), it points that there exist an removable device, so the system will choose the removable device first. But if there exists a non_removable sd card, it's called internal storage, isn't it? Or I misunderstand the definition of internal storage...
  • MattDavis
    MattDavis almost 11 years
    All Android phones require some type of storage for the OS, apps, etc. This internal storage is the location of "/data/data" and access is controlled by the OS. An SD card that provides additional storage is considered external storage whether it's removable or not. Most phones nowadays don't have a removable SD card slot, but rather come with an internal SD card along with a small amount of internal storage. This non-removable SD card behaves exactly the same, including being recognized as MEDIA_MOUNTED, as a removable SD card, and is separate from the phone's internal storage.
  • Alston
    Alston almost 11 years
    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...
  • MattDavis
    MattDavis almost 11 years
    Basically. The internal storage contains a bunch of things, notably the "/data" directory where Apps keep their private files. And you're right, it's somewhat misleading now, but made more sense back when Android was newer and most phones came equipped with small internal storage and SD card slots for expansion.
  • Abdul Rahman
    Abdul Rahman over 10 years
    @MattDavis: Hi, the github link is dead