Handling Images with ArrowDB in Titanium app

Posted By : Pawanpreet Singh | 29-Jun-2015

There are various times when we need to save a Photo in database, ArrowDB also provides us same to save the users uploaded image to server and if user needs it user can get the link of s3 which he/she can use in its app.

Here I will show you how to get from device and save it in ArrowDB

Getting Image from device


var mediaParams = {
	success : handlePhoto,
	cancel : function() {
     //do nothing
	},
	error : function(error) {
		 alert(error);
	},
	mediaTypes : Ti.Media.MEDIA_TYPE_PHOTO
};
 
Titanium.Media.openPhotoGallery(mediaParams);

function handlePhoto(e){
  var file = Titanium.Filesystem.getFile(e.media.nativePath);
  sendPhoto(file);
}
 

Here we are selecting an image from gallery and opening then sending the selected image to sendPhoto which will send this selected image to the server and is shown below, but before using this user must be logged in before calling the photo api.

Saving Photo


function sendFile(file){
Cloud.Photos.create({
    photo: Titanium.Filesystem.getFile('photo.jpg')
}, function (e) {
    if (e.success) {
        var photo = e.photos[0];
        alert('Success:\n' +
            'id: ' + photo.id + '\n' +
            'filename: ' + photo.filename + '\n' +
            'size: ' + photo.size,
            'updated_at: ' + photo.updated_at);
    } else {
        alert('Error:\n' +
            ((e.error && e.message) || JSON.stringify(e)));
    }
});
}
 

Now the photo is stored ArrowDB, Its time to fetch for use in app

Fetching Photo

 Cloud.Photos.show({
    photo_id: PhotoIdWhichWasReturnedWhileSaving
}, function (e) {
    if (e.success) {
        var photo = e.photos[0];
        alert('Success:\n' +
            'url: ' + photo.url + '\n'
    } else {
        alert('Error:\n' +
            ((e.error && e.message) || JSON.stringify(e)));
    }
});
 

 

You can use this image url in your app to show this image.

THANKS

 

About Author

Author Image
Pawanpreet Singh

Pawanpreet Singh is a technical project manager with experience in Web and Mobile apps development . Pawanpreet loves playing games on PS4 and going out with friends in free time.

Request for Proposal

Name is required

Comment is required

Sending message..