Phonegap/Cordova 无法下载到 Android 中的子文件夹
Posted
技术标签:
【中文标题】Phonegap/Cordova 无法下载到 Android 中的子文件夹【英文标题】:Phonegap/Cordova cannot download to subfolder in Android 【发布时间】:2012-11-19 21:07:25 【问题描述】:我正在使用 Phonegap 2.2.0 在 Eclipse 中构建一个 android 应用
这适用于 ios:
var uri = encodeURI(value);
var fileName = uri.substring(uri.lastIndexOf('/')+1);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem)
fileSystem.root.getDirectory("dir/subdir", create: true, exclusive: false, function(dirEntry)
dirEntry.getFile(fileName, create: true, exclusive: false, function(fileEntry)
var localPath = fileEntry.fullPath;
var fileTransfer = new FileTransfer();
fileTransfer.download(
uri,
localPath,
function(entry)
console.log("xfg download complete: " + entry.fullPath);
,
function(error)
console.log("xfg download error source " + error.source);
console.log("xfg download error target " + error.target);
console.log("xfg upload error code" + error.code);
);
);
);
);
在上述代码的第 4 行,我在“dir/subdir”处获取目录,并且下载工作正常。然而,在 Android 中,fileSystem 获取了子目录,但下载失败并显示“找不到文件”。
如果我将“dir/subdir”替换为“dir”,它会起作用。
对此有任何解决方案或巧妙的变通办法吗?
【问题讨论】:
【参考方案1】:您可以通过探测navigator
对象的userAgent
属性来识别设备类型:
if((navigator.userAgent.match(/Android/i)) == "Android")
如果是 Android 设备,请使用 dir
而不是 dir/subdir
。
见:Detect device type in phonegap
【讨论】:
【参考方案2】:您不能指定子目录,除非该目录已经存在。所以创建getDirectory('dir'...
然后getDirectory('subdir'...
https://developer.mozilla.org/en-US/docs/DOM/File_API/File_System_API/DirectoryEntry:
从 DirectoryEntry 到的绝对路径或相对路径 要查找或创建的目录。尝试是错误的 创建一个其直接父级尚不存在的文件。
【讨论】:
以上是关于Phonegap/Cordova 无法下载到 Android 中的子文件夹的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Phonegap / Cordova 3.5.0-0.2.4 上运行联系人插件
Phonegap/Cordova InAppbrowser 文件下载问题
Phonegap (Cordova) 2.7.0 + Mapkit 插件 + 入门