Guest User

Untitled

a guest
Jul 17th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. //Global instance of DirectoryEntry for our data
  2. var DATADIR;
  3. var knownfiles = [];
  4.  
  5. //Loaded my file system, now let's get a directory entry for where I'll store my crap
  6. function onFSSuccess(fileSystem) {
  7. //alert("onFSSuccess");
  8. fileSystem.root.getDirectory("Android/data/miApp",{create:true},gotDir,fail);
  9. }
  10. function fail(evt) {
  11. alert(evt.target.error.code);
  12. }
  13.  
  14. //The directory entry callback
  15. function gotDir(d){
  16. //alert("got dir");
  17. DATADIR = d;
  18. var reader = DATADIR.createReader();
  19. reader.readEntries(function(d){
  20. gotFiles(d);
  21. appReady();
  22. },onError);
  23. }
  24.  
  25. //Result of reading my directory
  26. function gotFiles(entries) {
  27. alert("The dir has "+entries.length+" entries.");
  28. for (var i=0; i<entries.length; i++) {
  29. //alert(entries[i].name+' dir? '+entries[i].isDirectory);
  30. knownfiles.push(entries[i].name);
  31. renderPicture(entries[i].toURL());
  32. }
  33. }
  34.  
  35. function renderPicture(path){
  36. $("#photos").append("<img style='max-width:200px;' src='"+path+"'>");
  37. //alert("<img src='file://"+path+"'>");
  38. }
  39.  
  40. function onError(e){
  41. alert("ERROR");
  42. //alert(JSON.stringify(e));
  43. alert("download error source " + e.source);
  44. alert("download error target " + e.target);
  45. alert("upload error code" + e.code);
  46. }
  47.  
  48. function onDeviceReady() {
  49. //what do we have in cache already?
  50. $("#status").html("Checking your local cache....");
  51. window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, onError);
  52. }
  53.  
  54. function appReady(){
  55. $("#status").html("Ready to check remote files...");
  56. $.get("http://www.raymondcamden.com/demos/2012/jan/17/imagelister.cfc?method=listimages", {}, function(res) {
  57. if (res.length > 0) {
  58. $("#status").html("Going to sync some images...");
  59. for (var i = 0; i < res.length; i++) {
  60. if (knownfiles.indexOf(res[i]) == -1) {
  61. //alert("need to download " + res[i]);
  62. var ft = new FileTransfer();
  63. var dlPath = DATADIR.toURL() + "/" + res[i];
  64. var uri= "http://www.raymondcamden.com/demos/2012/jan/17/" + escape(res[i]);
  65. //alert("STATE #1:"+uri);
  66. uri = encodeURI(uri);
  67. //alert("STATE #2:"+uri);
  68. console.log("downloading crap to " + dlPath);
  69. ft.download(uri, dlPath, function(e){
  70. renderPicture(e.toURL());
  71. console.log("Successful download of "+e.toURL());
  72. }, onError, true);
  73. }
  74. }
  75. }
  76. $("#status").html("");
  77. }, "json");
  78.  
  79. }
  80.  
  81. function init() {
  82. document.addEventListener("deviceready", onDeviceReady, true);
  83. //window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, onError);
  84. }
Add Comment
Please, Sign In to add comment