【发布时间】:2014-07-07 12:10:26
【问题描述】:
在安卓上我用过
file:///storage/sdcard0/Android/data/my.app.id/cache/
下载 (FileTransfer) 一些图像,然后在我的 html 中显示它们。删除我的应用程序时,此文件夹中的图像和其他所有内容都会被删除,这正是我想要的。
如何在 iOS 上实现这一点?
我试过了
file:///var/mobile/Applications/<GUID of app>/Documents/
因为这是我在请求文件系统时得到的,但图像甚至不会下载(错误代码 1)。有任何想法吗?我发现了有关我的错误的更多信息:它与this 问题中所述的错误相同。 (无法创建保存下载文件的路径 - Cocoa 错误 512)
谢谢
更多信息: 我使用的相关插件是
<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.file-transfer" />
和特点:
<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="File">
<param name="ios-package" value="CDVFile" />
</feature>
<feature name="FileTransfer">
<param name="ios-package" value="CDVFileTransfer" />
</feature>
我在 Android 中成功下载了图片:
var fileTransfer = new FileTransfer();
var uri = encodeURI("myserverurl/"+fileName);
var filePath = appCachePath+fileName;
fileTransfer.download(
uri,
filePath,
function(entry) {
alert("download complete: " + entry.fullPath);
},
function(error) {
alert("download error source/target/code:\n" + error.source +" \n||| "+error.target+" \n||| "+error.code);
}
);
我在成功获得 FS 后下载它们
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
if(device.platform === 'Android'){
cacheFolderSubPath = "Android/data/id.app.my/cache/";
}
else{
cacheFolderSubPath = ""; // for iOS what ??
}
}
function gotFS(fileSystem) {
var nturl = fileSystem.root.toNativeURL(); // returns cdvfile://localhost/persistent/ for both iOS and Android
window.resolveLocalFileSystemURL(nturl+cacheFolderSubPath, onResolveSuccess, onResolveFail);
}
function onResolveSuccess(fileEntry) {
// this is "file:///..." string mentioned in the question above.
appCachePath = fileEntry.toNativeURL()+"/";
// etc etc etc ... (use this appCachePath with download code above)
}
【问题讨论】:
-
发布您正在使用的代码以及您使用的任何 PhoneGap 库。
标签: android ios cordova phonegap-build