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

发布于 2025-01-05 09:50:58 字数 2431 浏览 1 评论 0原文

我让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生来就爱笑 2025-01-12 09:50:58

好吧,弄清楚了。问题是该文件是 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();
            }
        }
    });
});

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:

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();
            }
        }
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文