Search code examples
androidiostitaniumappceleratormedia

Titanium - Send picture to server : file size bug


I'd like to send pictures, from my app (iOS and Android) to my server. My code works with small pictures, but if the size is too big, when I send the data, nothing happens and the application slows down. Could you explain me the problems in my code and how to resolve it ? Thanks a lot :)

Here is my code :

var attached_media = [];
var file_btn = Ti.UI.createButton({ title: L('select') });

file_btn.addEventListener('click',function(e){
    Titanium.Media.showCamera({
        success:function(e) {
            if(e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
                attached_media.push(Ti.Utils.base64encode(e.media).text);
            }
        },
        saveToPhotoGallery:true,
        allowEditing: false,
        mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO]
    });
});

var send_button = Titanium.UI.createButton({
   title: 'Send',
});

send_button.addEventListener('click',function(e){

    var req = ......
    req.send({ 'medias':JSON.stringify(attached_media), 'user_id':Ti.App.Properties.getInt('user_id')});

});

I removed the unnecessary code, because it was too long ! :)


Solution

  • What I was able to understand from the provided info is that you are having problems while uploading large size pics, like the one from camera which turns out to be more than 2-3MB.

    The only solution at present I can suggest you is to compress the image using this iOS-Android module Ti-ImageFactory before saving or sending it to server.

    I recommend to compress the image right after you captured it in Camera's success callback like this:

    file_btn.addEventListener('click',function(e){
        Titanium.Media.showCamera({
            success:function(e) {
                if(e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
                    Ti.API.info("Initial pic bytes = " + e.media.length);
    
                    // if bytes length of pic is larger than 3MB or 3145728 bytes, set compression to 0.5,
                    // else keep it to default which is 0.7
                    var imf = require('ti.imagefactory');
                    var compressedPic = (e.media.length > 3145728) ? imf.compress(0.5) : imf.compress();
                    attached_media.push(Ti.Utils.base64encode(compressedPic).text);
    
                    Ti.API.info("Compressed pic bytes = " + compressedPic.length);
    
                    compressedPic = null;
                }
            },
            saveToPhotoGallery:true,
            allowEditing: false,
            mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO]
        });
    });
    

    Added Code - If captured picture size is more than 3MB, then compress it by 0.5 level, else compress it using default level 0.7. Also checking the initial pic size & compressed pic size to match better results as per app's requirements for faster uploading.

    You can also pass a compression level in compress() method. See docs for more.