Appcelerator:如何使用android上传视频?

时间:2012-02-13 14:55:05

标签: appcelerator titanium-mobile appcelerator-mobile

我使用Titanium.Media.showCamera()函数使iphone / ipad正常工作。哪个好。

然而,相同的代码不能像我期望的那样在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']));

1 个答案:

答案 0 :(得分:2)

好的想通了。问题是文件是uri,代码不读取文件系统上的uri。话虽如此,您必须将文件复制到新文件,然后使用该新文件上传到服务器。

以下解决方案适用于我:

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;




                var xhr = Titanium.Network.createHTTPClient({enableKeepAlive:false});
                xhr.open('POST', 'http://something.com/video/uploader.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 source = Ti.Filesystem.getFile(dataUri);
                var fileData = Ti.Filesystem.getFile('appdata://sample.3gp');
                // note: source.exists() will return false, because this is a URI into the MediaStore.
                // BUT we can still call "copy" to save the data to an actual file
                source.copy(fileData.nativePath);
                Titanium.Media.saveToPhotoGallery(fileData);
                if(fileData.exists())
                {
                    var fileContent = fileData.read();
                    if(fileContent)
                        xhr.send({video: fileContent});
                    else
                        alert('Did not get any data back from file content');
                }
                else
                    alert('Did not get a file data for : '+dataUri);
            } else {
                Ti.UI.createNotification({
                    duration: Ti.UI.NOTIFICATION_DURATION_LONG,
                    message: 'Canceled/Error? Result code: ' + e.resultCode
                }).show();
            }
        }
    });
});