在PhoneGap中获取设备的绝对路径?

4
我将使用Phonegap 3.3.0进行翻译,之前我使用的是2.5.0版本,在旧版本中,entry.fullPath可以给出设备的完整路径。这些路径通常会像这样:
/var/mobile/Applications/<application UUID>/Documents/path/to/file  (iOS)
/storage/emulated/0/path/to/file                                    (Android)

由于该方法已被弃用,我正在使用entry.toURL()方法来获取路径。此方法现在将返回以下形式的文件系统URL:

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

在我的应用程序中,我将URL传递给本机函数,然后从本机打开文件。但是如果我将路径传递给本机,则iOS无法检测到该文件。如果我硬编码绝对路径,则应用程序可以检测到相同的文件。
如何使用fileSystem URL访问本机中的文件或任何其他方法来获取设备的绝对路径?
提前感谢。
3个回答

7
toURL() 方法或者 nativeURL 属性应该返回所需的结果:
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!");
});

使用cordova 3.5,在iPad模拟器中的输出如下:
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/

在 Android 模拟器 (Genymotion) 中的表现是:

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

1
非常完美,谢谢。提醒其他人的是,这需要在设备准备好时调用:document.addEventListener("deviceready", onDeviceReady, false); 然后在您的设备准备好回调函数中放置 window.requestFileSystem 调用,例如:function onDeviceReady(){...} - Brett Gregson

0
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);
        }
    );
}

使用 fileSystem.root.nativeURL。 - kris

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

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

这将会给出以下路径 cdvfile://localhost/persistent/path/to/file..我想要绝对路径。 - Mohammed Imran N

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接