I got iphone/ipad to work fine with the Titanium.Media.showCamera() function. Which is great.
However the same code doesn't work on android like I expect. So I did some research and came up with this code below. The code itself works up to uploading the video. I can record, click save, but when it comes time to upload to my server, I get no communication error and on the server itself, I see no data in the POST or FILES array. The code below executes on the onclick button. I give part of the code since everything works except this. What gives?
button2.addEventListener('click', function() {
// http://developer.android.com/reference/android/provider/MediaStore.html
var intent = Titanium.Android.createIntent({ action: 'android.media.action.VIDEO_CAPTURE' });
Titanium.Android.currentActivity.startActivityForResult(intent, function(e) {
if (e.error) {
Ti.UI.createNotification({
duration: Ti.UI.NOTIFICATION_DURATION_LONG,
message: 'Error: ' + e.error
}).show();
} else {
if (e.resultCode === Titanium.Android.RESULT_OK) {
var dataUri = e.intent.data;
Titanium.Media.saveToPhotoGallery(dataUri);
var xhr = Titanium.Network.createHTTPClient({enableKeepAlive:false});
xhr.open('POST', 'http://someserver.com/upload.php');
xhr.setRequestHeader("enctype", "multipart/form-data");
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.onerror = function(e) {
alert(e.error);
};
xhr.onload = function() {
var data = JSON.parse(this.responseText);
if(data.FILE)
alert('File: '+data.FILE);
else
alert(this.responseText);
};
var fileData = Titanium.Filesystem.getFile(dataUri);
var fileContent = fileData.read();
xhr.send({video: fileContent});
} else {
Ti.UI.createNotification({
duration: Ti.UI.NOTIFICATION_DURATION_LONG,
message: 'Canceled/Error? Result code: ' + e.resultCode
}).show();
}
}
});
});
Also if you're interested in the php code, here it is:
<?php
file_put_contents('output.txt', print_r($_POST, true)."\n".print_r($_FILES, true));
if(empty($_FILES['video']))
die('invalid');
@move_uploaded_file($_FILES['video']['tmp_name'], $_FILES['video']['name']);
echo json_encode(array('FILE' => $_FILES['video']['name']));