Appcelerator:如何使用android上传视频?
我让 iphone/ipad 可以与 Titanium.Media.showCamera() 函数一起正常工作。这太棒了。
然而,同样的代码并不像我期望的那样在 android 上运行。所以我做了一些研究并提出了下面的代码。该代码本身可以上传视频。我可以录制,单击“保存”,但是当上传到我的服务器时,我没有收到任何通信错误,并且在服务器本身上,我在 POST 或 FILES 数组中看不到任何数据。下面的代码在 onclick 按钮上执行。我给出了部分代码,因为除了这个之外一切都有效。什么给?
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();
}
}
});
});
另外,如果您对 php 代码感兴趣,这里是:
<?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']));
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']));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,弄清楚了。问题是该文件是 uri,并且代码不会读取文件系统上的 uri。话虽如此,您必须将文件复制到新文件,然后使用该新文件上传到服务器。
下面的解决方案对我有用:
Okay figured it out. The issue is the file is a uri, and the code doesn't read uri on the file system. With that said, you'll have to copy the file to a new file and then use that new file to upload to a server.
The solution below works for me: