Mohammed Imran N
Mohammed Imran N

Reputation: 1319

Get Device absolute path in phonegap?

I'm using Phonegap 3.3.0, earlier i used 2.5.0 where entry.fullPath will give the Device full path. These paths would typically look like

/var/mobile/Applications/<application UUID>/Documents/path/to/file  (iOS)
/storage/emulated/0/path/to/file                                    (Android)

since that method is deprecated am using entry.toURL() method to get the path.This method will now return filesystem URLs of the form

cdvfile://localhost/persistent/path/to/file

In My application, am passing the URL to Native function and from native am opening a file. But If I pass the path to Native, iOS could not able to detect the file. The same If i hardcode the Absolute path, application detects the file.

How to use the fileSystem URL to access the file in native or any other method to get the device absolute path ?

Thanks in advance.

Upvotes: 4

Views: 13637

Answers (3)

Darshan
Darshan

Reputation: 2379

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    console.log("device is ready");
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function fail() {
    console.log("failed to get filesystem");
}

function gotFS(fileSystem) {
    console.log("got filesystem");

        // save the file system for later access
    console.log(fileSystem.root.fullPath);
    window.rootFS = fileSystem.root;
}

function downloadImage(url, fileName){
    var ft = new FileTransfer();
    ft.download(
        url,
        window.rootFS.fullPath + "/" + fileName,
        function(entry) {
            console.log("download complete: " + entry.fullPath);

        },
        function(error) {
            console.log("download error" + error.code);
        }
    );
}

Upvotes: 0

leo
leo

Reputation: 1253

The toURL() method or the nativeURL attribute should return the result wanted:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
    console.log("fileSystem.root.toURL()="+fileSystem.root.toURL());
    console.log("fileSystem.root.toInternalURL()="+fileSystem.root.toInternalURL());
    console.log("fileSystem.root.nativeURL="+fileSystem.root.nativeURL);
}, function(){
    alert("fails!");
});

Using cordova 3.5, the output in iPad simulator is:

fileSystem.root.toURL()=file:///Users/myuser/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/1717CB5F-4032-45C7-8CA2-342502447F36/Documents/
fileSystem.root.toInternalURL()=cdvfile://localhost/persistent/
fileSystem.root.nativeURL=file:///Users/myuser/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/1717CB5F-4032-45C7-8CA2-342502447F36/Documents/

...and in Android simulator (Genymotion) is:

fileSystem.root.toURL()=file:///storage/emulated/0/ 
fileSystem.root.toInternalURL()=cdvfile://localhost/persistent/ 
fileSystem.root.nativeURL=file:///storage/emulated/0/ 

Upvotes: 7

user859952
user859952

Reputation:

window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function gotFS(fileSystem) {
  alert("entered gotFS: " + fileSystem.root.toURL);
}

Upvotes: 0

Related Questions