10

Using the file api of phone gap build, I want to get the application directory in which all files should be stored.

What command do I need to use?

Neeraj
  • 8,408
  • 8
  • 41
  • 69

2 Answers2

4
function gotFS(fileSystem) {
    console.log("got filesystem");
    // save the file system for later access
    console.log(fileSystem.root.fullPath);
    window.rootFS = fileSystem.root;
}

document.addEventListener('deviceready', function() {                
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}, false);

With this code you'll have a DirectoryEntry object for the root directory of your app stored in the global variable "rootFS". You can get the path of that folder like this:

rootFS.fullPath
davids
  • 6,259
  • 3
  • 29
  • 50
  • I guess the fileSystem.root gives the location of "/sdcard". Atleast that happens when I am using phone gap build. – Neeraj Aug 01 '12 at 16:40
  • 2
    I am running into the same problem! I am working with phonegap build and Android on a Samsung sII - I end up in the filesystem.root (/sdcard) and not in the application folder :( some more suggestions on that? – Sidrich2009 Sep 11 '12 at 15:27
  • 1
    PhoneGap considers the /sdcard as a persistent directory if it discovers one, otherwise it will use the /data/... where your application resides – allesmi Oct 16 '12 at 11:37
  • @davids I get "" for fullPath and cdvfile://localhost/persistent with .toURL(), any ideas? :/ – trainoasis Jun 19 '14 at 09:40
  • 1
    @allesmi how can I force not to use /sdcard and instead use path where my app resides? – trainoasis Jun 19 '14 at 10:17
  • @trainoasis Did you find any solution? I want to force it to use the app path not external storage path. – mc9 Oct 27 '15 at 05:03
  • anyone found a solution for this? I need to force it to use the app's path as opposed to using the external path! – David Hope Oct 28 '17 at 15:34
3

You can use cordova.file.applicationDirectory for getting your app directory. It will resolve to file:///android_asset/ in android and /var/mobile/Applications//.app/ in IOS

You can get all your app files in the www directory in both android and iOS.

window.resolveLocalFileSystemURL(cordova.file.applicationDirectory+'www/index.html',success,fail);
function success(fileEntry){
console.log("got the file object")
}
function fail(error){
console.log("error");
}

for getting the app directory on the filesystem where your app files will be stored can be get by cordova.file.applicationStorageDirectory which further contains two directories files and cache both are persistent.

you can check the link for further details..

https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md

Satpal Tanan
  • 1,108
  • 7
  • 17