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. :-)
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.
-
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(); });
-
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 !! :-)
-
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?