Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

I can't access to files created in applicationDataDirectory

Hi everyone

In my app, I tried to access to remote image saved in filesystem > applicationDataDirectory on iPhone Simulator or iPhone device.

Images are saved, I saw in the app directory, but I've got the error :

[ERROR] Failed to load image: file://localhost/Users/xxxxxxxx/Library/Application%20Support/iPhone%20Simulator/4.0.1/Applications/C2425808-DD8C-472A-94A5-326071DBB4C4/Documents/, Error: Error Domain=ASIHTTPRequestErrorDomain Code=1 "A connection failure occurred" UserInfo=0x7a85bb0 {NSUnderlyingError=0x7a7fab0 "The operation couldn’t be completed. Connection refused", NSLocalizedDescription=A connection failure occurred}

It works in Ressources Directory.

Someone know this issue ?

PS : Appcelerator really, really, ROCKS. Congrats to all the appcelerator team. :-)

— asked October 15th 2010 by Renaud E
  • applicationdatadirectory
  • filesystem
0 Comments

4 Answers

  • Can you post a bit of code to see how you are trying to load an image? Usually you do not need to put any path information to access an image that is in the application directory so you can say something like

    var imageView = Ti.UI.createImageView({image: 'test.png'});
    

    You also need to be aware that an iPhone app will flatten the folders and by flatten I mean that the images will no longer be in a subfolder but in the root of the application folder itself.

    — answered October 16th 2010 by John McKnight
    permalink
    2 Comments
    • Thanks John.

      If I don't put a path, the file is saved in the "Ressources" folder and it works.

      I think that is the "cleaner" solution to put in the ApplicationDataDirectory? Or I'm wrong?

      Here is the code used (found in this forum and modified) :

      var get_file =  function(filename, url, fn_end ) {
          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;
              alert(file_obj.path);
              fn_end(file_obj);
          }
          else {
      
              if ( Titanium.Network.online ) {
                  var c = Titanium.Network.createHTTPClient();
      
                  //c.setTimeout(10000);
                  c.onload = function()
                  {
      
      
                          Ti.API.info('finished downloading '+ filename +' from '+url);
      
                          var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
                          f.write(this.responseData);
                          file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator;
      
                      fn_end(file_obj);
      
                  };
      
                  c.open('GET',url);
                  c.send();           
              }
      
      
      
          }
      };
      
      
      
      get_file("appcelerator.png","http://developer.appcelerator.com/assets/img/DEV_titmobile_image.png",createimage);
      

      — commented October 16th 2010 by Renaud E
    • Is there a reason when I use a dynamic filename ("filename" like in your example) that I get the connection refused error?

      var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
      

      that gives me an error, but

      var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'filename.png');
      

      works fine, of course with all the items with the same filename.

      iphone 4.1, latest appcelerator.

      Any thoughts?

      — commented October 19th 2010 by Chris Campbell
  • I had a wordy explanation of file structure and then I reread the first question. Did you edit out the filename itself in the debug output or is it really missing? I assume what is happening is you are using the value returned in the callback function which may just be the path. Have you tried appending the filename to it?

    For example I am using the same basic function to fetch an MP3 and here is how I am doing it. I build the full path to ensure I am accessing what I think I am after.

              get_remote_file('tts.mp3', audio_uri, function() {
                var sound = Ti.Media.createSound({url: Ti.Filesystem.applicationDataDirectory + '/tts.mp3'});
                sound.play();
              });
    
    — answered October 16th 2010 by John McKnight
    permalink
    0 Comments
  • OHHHH…

    Thanks John for your last comment.

    It allows me to understand that I forgot the filename, in the file path, when the file exist in the applicationDataDirectory

    Here is the right code :

    var get_file =  function(filename, url, fn_end ) {
        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+filename;
            alert(file_obj.path);
            fn_end(file_obj);
        }
        else {
    
            if ( Titanium.Network.online ) {
                var c = Titanium.Network.createHTTPClient();
    
                //c.setTimeout(10000);
                c.onload = function()
                {
    
    
                        Ti.API.info('finished downloading '+ filename +' from '+url);
    
                        var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
                        f.write(this.responseData);
                        file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator;
    
                    fn_end(file_obj);
    
                };
    
                c.open('GET',url);
                c.send();           
            }
    
    
    
        }
    };
    
    
    
    get_file("appcelerator.png","http://developer.appcelerator.com/assets/img/DEV_titmobile_image.png",createimage);
    

    Thanks again for your help !! :-)

    — answered October 16th 2010 by Renaud E
    permalink
    1 Comment
    • You're welcome, glad to help.

      — commented October 18th 2010 by John McKnight
  • Does anyone know what is the right path??? I don't see any valid answer for:

    app.js

    backgroundImage: 'MY_DIRECTORY/MY_PICTURE.png'

    this is crazy it works in the emulator but not on the device. Could anyone help on this please?

    — answered December 17th 2011 by all fou
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.