【问题标题】:How to record video and upload to server using Android?如何使用 Android 录制视频并上传到服务器?
【发布时间】:2016-02-20 01:36:27
【问题描述】:

我正在制作一个 Android 应用程序,用户将在其中录制一段短视频,当他们完成录制后,视频会自动上传到服务器。我找到了一个完全符合我要求的教程,但使用的是图片而不是视频。我会以同样的方式处理视频吗?或者如何更改教程中的代码以使其用于视频而不是图片?

教程链接:https://www.youtube.com/watch?v=3tEiiUOLemA

【问题讨论】:

  • 我看着它,看起来很眼熟。这是 youtube 上的教程,似乎大部分代码已被弃用。我没看过这个帖子。我将再次尝试教程,看看这个问题是否有帮助。谢谢!

标签: android video file-upload


【解决方案1】:

我已使用以下代码成功将视频从 Android 上传到网络服务器。

请注意,Android 中有多个用于 HTTP 通信的替代库 - 对于视频,您需要确保选择的任何一个(如果您不想使用下面的那个)支持 Multipart 消息。 Android 中 HTTP 的一个很好的概述在这里:https://packetzoom.com/blog/which-android-http-library-to-use.html

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.util.Log;

public class VideoUploadTask extends AsyncTask<String, String, Integer> {
    /* This Class is an AsynchTask to upload a video to a server on a background thread
     * 
     */

    private VideoUploadTaskListener thisTaskListener;
    private String serverURL;
    private String videoPath;

    public VideoUploadTask(VideoUploadTaskListener ourListener) {
        //Constructor
        Log.d("VideoUploadTask","constructor");

        //Set the listener
        thisTaskListener = ourListener;
    }

    @Override
    protected Integer doInBackground(String... params) {
        //Upload the video in the background
        Log.d("VideoUploadTask","doInBackground");

        //Get the Server URL and the local video path from the parameters
        if (params.length == 2) {
            serverURL = params[0];
            videoPath = params[1];
        } else {
            //One or all of the params are not present - log an error and return
            Log.d("VideoUploadTask doInBackground","One or all of the params are not present");
            return -1;
        }


        //Create a new Multipart HTTP request to upload the video
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(serverURL);

        //Create a Multipart entity and add the parts to it
        try {
            Log.d("VideoUploadTask doInBackground","Building the request for file: " + videoPath);
            FileBody filebodyVideo = new FileBody(new File(videoPath));
            StringBody title = new StringBody("Filename:" + videoPath);
            StringBody description = new StringBody("Test Video");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("videoFile", filebodyVideo);
            reqEntity.addPart("title", title);
            reqEntity.addPart("description", description);
            httppost.setEntity(reqEntity);
        } catch (UnsupportedEncodingException e1) {
            //Log the error
            Log.d("VideoUploadTask doInBackground","UnsupportedEncodingException error when setting StringBody for title or description");
            e1.printStackTrace();
            return -1;
        }

        //Send the request to the server
        HttpResponse serverResponse = null;
        try {
            Log.d("VideoUploadTask doInBackground","Sending the Request");
            serverResponse = httpclient.execute( httppost );
        } catch (ClientProtocolException e) {
            //Log the error
            Log.d("VideoUploadTask doInBackground","ClientProtocolException");
            e.printStackTrace();
        } catch (IOException e) {
            //Log the error
            Log.d("VideoUploadTask doInBackground","IOException");
            e.printStackTrace();
        }

        //Check the response code
        Log.d("VideoUploadTask doInBackground","Checking the response code");
        if (serverResponse != null) {
            Log.d("VideoUploadTask doInBackground","ServerRespone" + serverResponse.getStatusLine());
            HttpEntity responseEntity = serverResponse.getEntity( );
            if (responseEntity != null) {
                //log the response code and consume the content
                Log.d("VideoUploadTask doInBackground","responseEntity is not null");
                try {
                    responseEntity.consumeContent( );
                } catch (IOException e) {
                    //Log the (further...) error...
                    Log.d("VideoUploadTask doInBackground","IOexception consuming content");
                    e.printStackTrace();
                }
            } 
        } else {
            //Log that response code was null
            Log.d("VideoUploadTask doInBackground","serverResponse = null");
            return -1;
        }

        //Shut down the connection manager
        httpclient.getConnectionManager( ).shutdown( ); 
        return 1;
    }

    @Override
    protected void onPostExecute(Integer result) {
        //Check the return code and update the listener
        Log.d("VideoUploadTask onPostExecute","updating listener after execution");
        thisTaskListener.onUploadFinished(result);
    }

}

【讨论】:

  • 如何以及从哪里获得 params[0] 和 params[1] 值?
  • 它们作为参数从任何调用任务的地方传入。它们会有所不同,具体取决于您要上传到的服务器以及视频在您设备上的位置。
【解决方案2】:

This is 在应用程序中实现此功能的更好教程。但是在 API 级别 24 之后现在不允许使用文件// 方案。因此,您需要 look here ,我希望通过这 2 个链接,您可以轻松实现这一点。

【讨论】:

  • 我使用了教程,现在我的应用程序和服务器正在运行。虽然它说上传成功,但我没有看到上传的文件。是文件//方案问题的 bc 吗?
  • 其实是上传的。但前提是文件大小很小!
猜你喜欢
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
相关资源
最近更新 更多