android - 在phonegap中获取设备绝对路径?

标签 android ios cordova

我使用的是 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/

相关文章:

android - 在 LinearLayout 中设置 EditText 宽度取决于 TextView 宽度

java - 打开应用程序, map 放大到当前位置

ios - 为什么 UIAlertView 的委托(delegate)不起作用?

php - iOS推送通知返回码102

ios - Phonegap 3.3.0 config.xml解析错误

cordova - Cordova 构建任务时获取 "TypeError: file argument must be a non-empty string"

android - 无法进行安卓构建

java - Android int 在 eclipse 中替换为 android.R.integer

ios - 将 Vision VNTextObservation 转换为字符串

ios - 带有 Cordova 垂直滚动的 Framework7 在 iOS 上不起作用