Search code examples
titaniumappceleratortitanium-mobileacs

Can I set the user's photo to Titanium ACS?


I need to add a photo to my user, by doing it from the web interface, I got the photo set to a field called photo , but if I try to do the same thing programmatically from the mobile app it doesn't work.

photonativePath is my photo url from camera or gallery.

Cloud.Users.update({
    email: '[email protected]',
    first_name: 'joe',
    last_name: 'user',
    photo : Titanium.Filesystem.getFile(photonativePath),
    custom_fields: {
        favorite_color: 'blue',
        age: 25
    }
}, function (e) {
    if (e.success) {
        var user = e.users[0];
        alert('Success:\n' +
            'id: ' + user.id + '\n' +
            'first name: ' + user.first_name + '\n' +
            'last name: ' + user.last_name);
    } else {
        alert('Error:\n' +
            ((e.error && e.message) || JSON.stringify(e)));
    }
});

Solution

  • Try the following, You can open the photoGallery using the openPhotoGallery method. This will open the image from the gallery and select the picture. It will update the picture in success callback

    function openPhotoGallery(){
        Ti.Media.openPhotoGallery({
            success: function (event){
                var image=event.media;
                var imgvwTest = Ti.UI.createImageView({
                    image : image
                });
                updatePhoto(imgvwTest.toImage());
            },
            cancel:function(){
                Titanium.UI.createAlertDialog({
                    title:'Error', 
                    message:'An error occured while trying to reference your gallery!'
                }).show();
            },
            error:function(er){
                Titanium.UI.createAlertDialog({
                    title:'Error', 
                    message:'An error occured while trying to reference your gallery!'
                }).show();
            }
        });
    
    }
    
    function updatePhoto(selectedImage){
    
        Cloud.Users.update({
            email: '[email protected]',
            first_name: 'joe',
            last_name: 'user',
            photo : selectedImage.media,
            custom_fields: {
                favorite_color: 'blue',
                age: 25
            }
        }, function (e) {
            if (e.success) {
                var user = e.users[0];
                alert('Success:\n' +
                    'id: ' + user.id + '\n' +
                    'first name: ' + user.first_name + '\n' +
                    'last name: ' + user.last_name);
            } else {
                alert('Error:\n' +
                    ((e.error && e.message) || JSON.stringify(e)));
            }
        });
    }
    

    Please let me know if you have any issues