Phonegap API には、リモート サーバーからダウンロードした画像を保存するために使用できるファイル システムがあります。
ファイルはアプリの Documents フォルダーに保存されるため、そのパス (インストールごとに異なります) を見つけて、ファイルをローカルに保存し、そのパスを localstorage に保存する必要があります。
コード スニペットを次に示します。まず、ローカル パスをワークアウトするために、dummy.html ファイルを作成して保存します。次に、ファイルをダウンロードします。
function downloadFile(webURL,webFilename){
window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry){
var sPath = fileEntry.fullPath.replace("dummy.html","");
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.onprogress = function(result){
var percent = result.loaded / result.total * 100;
percent = Math.round(percent);
console.log('Downloaded: ' + percent + '%');
};
fileTransfer.download(
webURL,
sPath + webFilename,
function(theFile) {
console.log("download complete: " + theFile.toURL());
showLink(theFile.toURL());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
navigator.notification.alert('Seems to be an error downloading this background. Try again later.', null, 'Error', 'OK');
}
);
},
fail);
},
fail);
}
function showLink(localurl){
console.log(localurl);
}