Having an issue uploading an image

You must Login before you can answer or comment on any questions.

This is working on the iPhone but for some reason with android the image uploads, it is the correct size but it is corrupted.

What i do below is take the photo, resize it using image factory (the image is fine after resize. Im looking at it on the phone) and then upload it to the server. Any ideas on whats going wrong?

tw.ui.takePhoto = function(vin){
 
 
        Titanium.Media.showCamera({
            success:function(event)
            {
                var cropRect = event.cropRect;
                var image = event.media;
 
                Ti.API.debug('Our type was: '+event.mediaType);
                if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
                {
                    Ti.App.fireEvent('show_indicator');
                    var image = event.media
 
                    var newImage  = resizeKeepAspectRatioNewHeight(image, 1937, 1937, 500);
                    var f = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,vin + '-' + currentPhotos + '.png');
                    f.write(newImage);
                    var data_to_send = { 
                        "image": f.read(), 
                        "name": vin + '-' + currentPhotos + '.png'
                    };
 
                    xhr = Ti.Network.createHTTPClient();
 
                    xhr.onsendstream = function(e)
                    {
                    if(!isAndroid){
                        var a = new Object;
                        var progress = e.progress;
                        progress = progress * 100
                        progress = parseInt(progress)
 
                        a.title = 'Loading ' + progress + '%' ;
                        Ti.App.fireEvent('change_title',a);
                       }
                    };
 
                    xhr.open("POST", siteurl + "app/uploadnew.html?name=" + vin + '-' + currentPhotos + '.png');
                    xhr.setRequestHeader("enctype", "multipart/form-data");
                    xhr.setRequestHeader("Content-Type", "image/png");
                    xhr.send(data_to_send); 
                    xhr.onload = function() {
 
                        var response = JSON.parse(this.responseText)
                        if(response.status == 'error'){
                            Ti.App.fireEvent('hide_indicator');
                            showError('Error',response.description);
                        }else{
                            currentPhotos = currentPhotos + 1
                            lblPhotos.text = 'Photos Taken:' + currentPhotos;
                            Ti.App.fireEvent('hide_indicator');
                        }
                    };
 
 
 
 
                }
                else
                {
                    alert("got the wrong type back ="+event.mediaType);
                }
            },
            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('Please run this test on device');
                }
                else
                {
                    a.setMessage('Unexpected error: ' + error.code);
                }
 
                // show alert
                a.show();
            },
            saveToPhotoGallery:false,
            allowEditing:true,
            mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]
        });
    }

— asked 11 months ago by chris smashe
2 Comments
  • more info. Found out is is because it is uploading the file as a jpeg file but its a incorrect encoded jpeg file. it will not show in a browser. It will open in photoshop (but i need to have it work without doing that), it will not open server side to modify it. I get the following error Not a JPEG file: starts with 0x6e 0x74 Any ideas?

    — commented 11 months ago by chris smashe

  • More into. The phone saves the images fine to the phone. The extension is .jpg. I can upload the image and it works fine in a browser. The image that gets uploaded with the above code (except i change it not to resize the image and the content type to .jpg and when the image gets uploaded it gets changed to .jpeg extension and the .jpeg file is not correctly encoded.

    — commented 11 months ago by chris smashe

Your Answer

Think you can help? Login to answer this question!