Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-5312

Android: Write remote images to disk fails

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Cannot Reproduce
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Android
    • Labels:
    • Environment:

      Titanium SDK version: 1.7.1 timestamp=06/21/11 14:28 githash=1293a6d
      Titanium Studio version: 1.0.2.201107130739
      Platform: Android 2.1, Android 2.2, Android 2.3.3
      Device: Android simulators and Android Devices
      Host: osx 1.6.6

      Description

      When download an image gallery and save it to the phones filesystem, problems occur.
      It works great with 30-50 images at the time. But a large amount of images resolves in some of the images are not saved to disk. The skipped images are random each time I test this. Sometimes it is only a few that are left out, other times its 20 images out of 150.
      Its the same behaviour on iOS and Android.

      Here is my json loader of the remote images:

       
      xhr.onload = function() {
          try {
              var items = eval('('+this.responseText+')');
              for (var c=1;c<items.length;c++)
              {           
                  var id = items[c].id;
                  var imageLarge = items[c].imageLarge;
                  var imageSmall = items[c].imageSmall;
                  var imageMedium = items[c].imageMedium; 
       
                  //image related content
                  var imageArr = [imageLarge, imageSmall, imageMedium];
                  var imageNameArr = ['imageLarge_', 'thumb_', 'thumb_'];
                  var imageNativePath = [];
       
                  for (var i=0; i < imageArr.length; i++) {
                      var filename = xtractFile(imageArr[i]);
                      var imageName = '' + imageNameArr[i] + id + filename.extension;
       
                      //SAVE REMOTE FILE NOW !
                      //get_remote_file( filename, url, debug, progress, override)
                      getRemoteFile('gallery/'+imageName, imageArr[i], function(fileobj){ Ti.API.info(fileobj) }, null, true );
                  };
       
              //all done 
              showContent();
          }
              catch(e) {
                  //something goes wrong - showcontent
                  trace("CATCH ERROR: " + e);
              }
          };
          xhr.onerror = function() {
              trace("ONERROR: " + e);
          };
          xhr.open("GET",rssFeed);
          xhr.send(); 
      };
      

      The below is my save to disk function. It works great when the app is handling 50 images at the time, but once its higher, some of the images are not stored, even though this below function is called.

       
      function getRemoteFile(filename, url, fn_end, fn_progress, override ) {
          var file_obj = {file:filename, url:url, path: null};
          var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
       
          if ( file.exists() ) {
              file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator;
              if(override) {
                  file.deleteFile();
              } else {
                  return;
              }     
          }
          if ( Titanium.Network.online ) {
              var c = Titanium.Network.createHTTPClient();
              c.setTimeout(10000);
              c.onload = function()
              {
                  if (c.status == 200 ) {
                      var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
       
                      //write file to disk
                      f.write(this.responseData);
       
                      if(f.size == 0) {
                          //size 0 - start over
                          trace("ERROR: image write, try again");
                          getRemoteFile(filename, url, fn_end, fn_progress, override )
                      }
       
                      file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator;
                  } else {
                      file_obj.error = 'file not found'; // to set some errors codes
                  }
              };
              c.ondatastream = function(e)
              {
                  if ( fn_progress ) fn_progress(e.progress);
              };
              c.error = function(e)
              {
       
                  file_obj.error = e.error;
                  fn_end(file_obj);
                  trace("error: " + e);
              };
              c.open('GET',url);
              c.send();           
          } else {
              file_obj.error = 'no internet';
              fn_end(file_obj);
          }  
      };
      

      json feed: http://cph.napp.dk/index.php?option=com_phocagallery&view=napp&format=json

      There are no errors or exceptions I can paste in, due to the fact that Titanium does not make "errors" it just skips a lot of files.
      Yes, it has nothing to do with the server. I have tried without the "write remote to disk functionality" and the gallery works great.
      Q&A Thread at save 100+ remote images to disk

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                hpham Hieu Pham
                Reporter:
                viezel Mads Moller
              • Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    TIMOB-4496
                    Sync status:
                    ERROR
                    Last received:
                    Last sent:

                    Git Integration