如何向服务器发送大尺寸视频?

How Can I Send Video of a large size to Server?

我找到了这段代码 ,它可以完美地处理视频、图像和音频。但是当我发送一个大视频时我得到这个错误:

Throwing OutOfMemoryError "Failed to allocate a 28 byte allocation with 0 free bytes and -1856B until OOM" (recursive case)

视频太大,无法一次在 RAM 中处理,因此您要么需要缩小文件,要么分段处理,在这种情况下,您的服务器需要配置为也接受流数据块。来自 HttpURLConnection 文档:

To upload data to a web server, configure the connection for output using setDoOutput(true). For best performance, you should call either setFixedLengthStreamingMode(int) when the body length is known in advance, or setChunkedStreamingMode(int) when it is not. Otherwise HttpURLConnection will be forced to buffer the complete request body in memory before it is transmitted, wasting (and possibly exhausting) heap and increasing latency.

因此设置 setChunkedStreamingMode() 可能会解决您的问题。

你也可以看看这个问题:Upload large file in Android without outofmemory error