Titanium Community Questions & Answer Archive

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

Can't take a picture, save it to file, then upload

I can't save a picture to a file and upload it. When I try to retrieve the saved pic, it won't read? This is so hard to troubleshoot because I can't test in the simulator…This is driving me bonkers!

Order of Events:

1) camera opens

2) take a pic and edit

3) store to temporary/local file

4) close camera and goes back to where user is inputting info

all this works fine

5) user clicks a button

6) app grabs variables and image (blob) to send via xhr

stuck here

7) sends xhr

Using the following code


        Titanium.Media.showCamera({
            success:function(event)
            {
                var cropRect = event.cropRect;
                var image = event.media;

var f = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory,'upload_photo.png');
                f.write(image);

                tableView.updateRow(4,{title: 'Snapped!', hasCheck:true});
                hasPic = true;
            },
            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,
            saveToPhotoGallery:false
        });

here's part of the xhr (it gets sent to server fine, picture is not included – it's like it can't read the file)


var fimg = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory, 'upload_photo.png');
var blob = fimg.read();
Ti.API.info(blob);
xhr.send({uid:user,gid:group,media:blob,cname:nameValue,cemail:emailValue,cphone:phoneValue,cnote:noteValue,cgeo:geoArray});

I've tried setting var image at the top of the js (trying to make it more global) and using image = event.image in the camera script and again in the xhr. No dice.

Please help

— asked April 14th 2010 by Ryan Gartin
  • blob
  • camera
  • iphone
  • mobile
  • xhr
2 Comments
  • Ryan, any chance that you can share the code!. or create a tutorial? That would be great! Thanks!

    — commented August 31st 2011 by Cesar Estrada
  • yes, can you share ?

    — commented February 14th 2012 by Gaurav Sood

4 Answers

  • Accepted Answer

    I've certainly done exactly what you're trying so I'm sure it works. Must be something going on with your particular case.

    The first thing I would check is to be absolutely sure that whatever you're using to receive this on the server-side is properly handling the incoming file. What kind of server is receiving this file? Is this a POST or a GET?

    — answered April 14th 2010 by Alan McConnell
    permalink
    0 Comments
  • Other variables are being received, but picture is coming in blank.

    I'm going to try one more thing: Add an alert popup (I can't see the Ti.log output) that pops up before send. I'll hopefully see a [TiiObject]…standby

    — answered April 14th 2010 by Ryan Gartin
    permalink
    0 Comments
  • Ok, the alert test was a good idea. I do have a popup that says [object TiBlob]. After it uploads, I get a Status of 200 then app crashes after I select Ok on an alert box.

    In the php I have the following:

    $_FILES['media'];
    

    The DB says the pic got upload and the contents are: [BLOB - 5B]

    — answered April 14th 2010 by Ryan Gartin
    permalink
    0 Comments
  • Alan, you were totally right, I'm way out of date on my PHP. HA! Too much Titanium/Javascript has made me rusty.

    Folks, on the PHP side you need this code:

    $data = file_get_contents($_FILES['media']['tmp_name']);
    $picture = mysql_real_escape_string($data);
    

    Thanks!

    — answered April 14th 2010 by Ryan Gartin
    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.