Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Global instance of DirectoryEntry for our data
- var DATADIR;
- var knownfiles = [];
- //Loaded my file system, now let's get a directory entry for where I'll store my crap
- function onFSSuccess(fileSystem) {
- //alert("onFSSuccess");
- fileSystem.root.getDirectory("Android/data/miApp",{create:true},gotDir,fail);
- }
- function fail(evt) {
- alert(evt.target.error.code);
- }
- //The directory entry callback
- function gotDir(d){
- //alert("got dir");
- DATADIR = d;
- var reader = DATADIR.createReader();
- reader.readEntries(function(d){
- gotFiles(d);
- appReady();
- },onError);
- }
- //Result of reading my directory
- function gotFiles(entries) {
- alert("The dir has "+entries.length+" entries.");
- for (var i=0; i<entries.length; i++) {
- //alert(entries[i].name+' dir? '+entries[i].isDirectory);
- knownfiles.push(entries[i].name);
- renderPicture(entries[i].toURL());
- }
- }
- function renderPicture(path){
- $("#photos").append("<img style='max-width:200px;' src='"+path+"'>");
- //alert("<img src='file://"+path+"'>");
- }
- function onError(e){
- alert("ERROR");
- //alert(JSON.stringify(e));
- alert("download error source " + e.source);
- alert("download error target " + e.target);
- alert("upload error code" + e.code);
- }
- function onDeviceReady() {
- //what do we have in cache already?
- $("#status").html("Checking your local cache....");
- window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, onError);
- }
- function appReady(){
- $("#status").html("Ready to check remote files...");
- $.get("http://www.raymondcamden.com/demos/2012/jan/17/imagelister.cfc?method=listimages", {}, function(res) {
- if (res.length > 0) {
- $("#status").html("Going to sync some images...");
- for (var i = 0; i < res.length; i++) {
- if (knownfiles.indexOf(res[i]) == -1) {
- //alert("need to download " + res[i]);
- var ft = new FileTransfer();
- var dlPath = DATADIR.toURL() + "/" + res[i];
- var uri= "http://www.raymondcamden.com/demos/2012/jan/17/" + escape(res[i]);
- //alert("STATE #1:"+uri);
- uri = encodeURI(uri);
- //alert("STATE #2:"+uri);
- console.log("downloading crap to " + dlPath);
- ft.download(uri, dlPath, function(e){
- renderPicture(e.toURL());
- console.log("Successful download of "+e.toURL());
- }, onError, true);
- }
- }
- }
- $("#status").html("");
- }, "json");
- }
- function init() {
- document.addEventListener("deviceready", onDeviceReady, true);
- //window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, onError);
- }
Add Comment
Please, Sign In to add comment