Titanium Community Questions & Answer Archive

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

openPhotoGallery and Titanium.Filesystem.getFile : Bug ?

Hey,
I try to select a picture with openPhotoGallery, and write with Titanium.Filesystem.getFile, after that i want to change the background of the windows…. It's work, but just the first time !

I check in / iphone simulator / user / …. / Documents /test.jpg at each change the image in the directory is good (!!), but not in the application (is always the first selected) ! As if he had a cache issue ….The background reload, but always with the same image (the first!).

When I reload the app, the image is the last selected (this confirm when I write with Filesystem it's work fine).

So…. It's very important for my app, please somebody can help me?
I use Titanium Developer 1.0 / SDK 1.0 and Iphone SDK : 3.1.3
Thank you

var win = Titanium.UI.currentWindow;
var backgd    = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,"test.jpg");
if(backgd.exists()){win.backgroundImage = backgd.nativePath;}


var add = Titanium.UI.createButton({systemButton:Titanium.UI.iPhone.SystemButton.ADD});
win.rightNavButton = add;    
add.addEventListener('click',function()
{        
    Titanium.Media.openPhotoGallery(
    {    
        success:function(event)
        {
            var cropRect = event.cropRect;
            var image = event.media;
            var thumbnail = event.thumbnail;            
            var BackgroundFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,"test.jpg");
            BackgroundFile.write(image);

            //DEBUG            
            Ti.API.info('file = ' + BackgroundFile);
            var contents = BackgroundFile.read();
            Ti.API.info("contents blob object = "+contents);
            Ti.API.info('contents = ' + contents.text);
            Ti.API.info('mime type = ' + contents.mimeType);
            Ti.API.info('nativePath = ' + BackgroundFile.nativePath);
            Ti.API.info('exists = ' + BackgroundFile.exists());
            Ti.API.info('size = ' + BackgroundFile.size);
            Ti.API.info('readonly = ' + BackgroundFile.readonly);
            Ti.API.info('symbolicLink = ' + BackgroundFile.symbolicLink);
            Ti.API.info('executable = ' + BackgroundFile.executable);
            Ti.API.info('hidden = ' + BackgroundFile.hidden);
            Ti.API.info('writable = ' + BackgroundFile.writable);
            Ti.API.info('name = ' + BackgroundFile.name);
            Ti.API.info('extension = ' + BackgroundFile.extension());
            Ti.API.info('resolve = ' + BackgroundFile.resolve());                

            win.backgroundImage = null;
            win.backgroundImage = BackgroundFile.nativePath;    
            Titanium.API.info('PHOTO GALLERY SUCCESS cropRect.x ' + cropRect.x + ' cropRect.y ' + cropRect.y  + ' cropRect.height ' + cropRect.height + ' cropRect.width ' + cropRect.width);
        },
        cancel:function()
        {

        },
        error:function(error)
        {
            Titanium.UI.createAlertDialog({title:'Erreur', message:'...'}).show();
        },
        //allowImageEditing:true,
        //mediaTypes: Titanium.Media.MEDIA_TYPE_PHOTO
    });
});
— asked March 17th 2010 by Lagarde Mickael
  • filesystem.getfile
  • openphotogallery
1 Comment
  • Did you ever get this to work?

    I'm having the same problem when I choose a picture from the gallery, set it as the image in an ImageView, and then try to choose a different image from the gallery for the same ImageView.

    — commented October 27th 2010 by Julius Moreland

4 Answers

  • Before you write the test.jpg file, you should write a test to see if it 'exists' first, then delete it if it does.

    Your code is probably not able to overwrite the first one.

    hope this helps,
    Kosso

    — answered March 17th 2010 by Kosso
    permalink
    0 Comments
  • How do I delete test.jpg?
    The second picture selected is saved in the cache of the application (see
    / iphone simulator / user / …. / Documents /test.jpg, after selecting the image, we can ensure that it's good)

    I had a reply on the old forum here :
    http://support.appcelerator.net/discussions/support/2368-10-titaniumfilesystemgetfile-bug

    If someone has an alternative to offer me ….
    Thank you.
    It's essential for my application, I can't finish because of this :(

    — answered March 17th 2010 by Lagarde Mickael
    permalink
    0 Comments
  • You can use this code for this.

    var image = event,media;
    
    var filename = new Date().getTime() + "-app.jpg";
    
    bgImage = Titanium.Filesystem.getFile(desigantionFile, filename);
    
    bgImage.write(image);
    
    — answered July 29th 2012 by shanuka gayashan
    permalink
    1 Comment
    • sorry

      var image = event,media;
      

      should

      var image = event.media;
      

      — commented July 29th 2012 by shanuka gayashan
  • Simply do a

    bgImage.deleteFile();
    

    … at the end of your code…

    — answered March 27th 2015 by Frank Wollweber
    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.