我使用的是 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 传递给 native 函数,并从 native 打开一个文件。但是如果我将路径传递给 Native,iOS 将无法检测到该文件。同样,如果我对绝对路径进行硬编码,应用程序会检测到该文件。
如何使用文件系统 URL 以 native 或任何其他方式访问文件以获取设备绝对路径?
提前致谢。
最佳答案
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/
关于android - 在phonegap中获取设备绝对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23309311/