Titanium Community Questions & Answer Archive

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

Accessing Images in PhotoGallery

Hello Community,

How do I save an image location that I have included in the the photogallery and display the image the next time the app is opened. Here is the scenario. I take a picture, save it to the photogallery and display it on the screen. I then close the app and when I come back to the app I want to be able to display the same picture from the photogallery. I don't see how to save the index to to the image for return use. Thanks for any and all help.

— asked March 18th 2010 by General Usage
  • image
  • mobile
  • photogallery
  • refrence
0 Comments

5 Answers

  • Take Picture:

    Titanium.Media.showCamera({
    
        success:function(event)
        {
            var cropRect = event.cropRect;
            var image = event.media;
            var thumbnail = event.thumbnail;
    
                    var filename = "camera.png";
            Titanium.Api.Properties.setString("filename", filename);
    
            var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
            f.write(image);
            win.backgroundImage = f.nativePath;
        },
        cancel:function()
        {
    
        },
        error:function(error)
        {
            // create alert
            var a = Titanium.UI.createAlertDialog({title:'Camera'});
    
            // set message
            if (error.code == Titanium.Media.NO_CAMERA)
            {
                a.setMessage('Device does not have video recording capabilities');
            }
            else
            {
                a.setMessage('Unexpected error: ' + error.code);
            }
    
            // show alert
            a.show();
        },
        allowImageEditing:true,
    });
    

    Then when you reload your application you can check if the file is there.

    var image = Ti.Ui.createImageView({
         height: "auto",
         width: "auto"
    });
    
    if (Ti.Api.Properties.getString("filename") != null) {
            // we have the file, so show it
            var filename = Ti.Api.Properties.getString("filename");
    
            var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
            image.url = f;
    }
    

    I didn't run this, so the syntax may be off a bit.

    — answered March 19th 2010 by Rob Edgell
    permalink
    0 Comments
  • Rob your code was close to working. There were a couple of case issues on API and UI and a couple of spots where API should have been App so I changed those and it worked beautifully.

    
    Titanium.Media.showCamera({
    
        success:function(event)
        {
            var cropRect = event.cropRect;
            var image = event.media;
            var thumbnail = event.thumbnail;
    
                    var filename = "camera.png";
            Titanium.App.Properties.setString("filename", filename);
    
            var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
            f.write(image);
            win.backgroundImage = f.nativePath;
        },
        cancel:function()
        {
    
        },
        error:function(error)
        {
            // create alert
            var a = Titanium.UI.createAlertDialog({title:'Camera'});
    
            // set message
            if (error.code == Titanium.Media.NO_CAMERA)
            {
                a.setMessage('Device does not have video recording capabilities');
            }
            else
            {
                a.setMessage('Unexpected error: ' + error.code);
            }
    
            // show alert
            a.show();
        },
        allowImageEditing:true
    });
    
    var image = Titanium.UI.createImageView({
         height: "auto",
         width: "auto"
    });
    
    if (Titanium.App.Properties.getString("filename") != null) {
            // we have the file, so show it
            var filename = Titanium.App.Properties.getString("filename");
    
            var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
            image.url = f;
    }
    
    win.add(image);
    
    — answered July 16th 2010 by Eric Rodrigue
    permalink
    1 Comment
    • Hi Eric! Awesome code. By any chance can you change the code that when there is no camera available it allows me to choose a image from the photo gallery.

      — commented December 31st 2010 by Justin Ko
  • I don't think the Url or Path to an image in the gallery is available in 1.0.

    I do something similar by doing the following:
    1) Save the image as a blob to the applicationDataDirectory
    2) Save a property letting me know the file name, and any other info
    3) Then save the image to the photogallery

    When I load my app, I check that property to see if the user has selected an image and load the image from the applicationDataDirectory

    Although more work then just referencing the photogallery it does allow you to keep the image even if the user removes it from their gallery.

    — answered March 18th 2010 by Ben Bahrenburg
    permalink
    0 Comments
  • Thanks Ben for the help. I was afraid that was the case. I just did not want to keep two copies of the same image on the phone but I think I will need to go with your solution.

    — answered March 18th 2010 by General Usage
    permalink
    0 Comments
  • Hi all, just getting started with Ti. Is it still the case that you cannot refer to the filesystem path of a photo in the photo gallery? It seems ridiculous to store a duplicate copy of the photo in the applicationDataDirectory. Anyone know if the Appcelerator folks have fixed this?

    — answered December 21st 2010 by Ken-ichi Ueda
    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.