android - 将文件上传到服务器时出现 OutOfMemoryError
问题描述
我正在尝试将一个大型视频文件(100MB)上传到服务器,但我收到了 OutOfMemoryError。以下适用于小尺寸文件。OutOfMemoryError 可能是什么问题?
@Override
public void writeTo(@NotNull BufferedSink sink) throws IOException {
Source source;
source = Okio.source(file);
try (Buffer buffer = new Buffer()) {
long remaining = contentLength();
long originalFileLength = contentLength();
for (long readCount; (readCount = source.read(buffer, DEFAULT_BUFFER_SIZE)) != -1; ) {
if (mMediaUploadService.isRetryShown())
break;
sink.write(buffer, readCount);
sink.flush();
fileSize = contentLength();
remainingBytes = remaining -= readCount;
LogMessage.d(TAG, "source size: " + contentLength() + " remaining bytes: " + (remainingBytes));
uploadTaskCompletedListener.onUploadProgressChanged(readCount, originalFileLength);
}
firstTimeCall[0] = true;
} catch (Exception e) {
LogMessage.e(TAG, "write to exception: = " + e.getMessage());
} finally {
Util.closeQuietly(source);
}
}
对于此代码,我收到以下错误
E/Logger: [RxSchedulerPurge-1(913): MobileApplication.kt:uncaughtException:78] - java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
E/Logger: [OkHttp Dispatcher(962): MobileApplication.kt:uncaughtException:78] - java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.mirrorfly.qa, PID: 17212
Theme: themes:{}
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
如何解决这个问题?
解决方案
推荐阅读
- node.js - Node.js 中是否有支持 `addEventListener()` 的工作人员?
- html - 如何编辑 ul li 字体大小和样式?
- python-3.x - 用于将 *args 转换为字符串的 Python 打印函数
- python - 试图找到断开图形的节点集,在python中使用networkx
- python - Pandas Q-Cut by Row
- python - 从多个 PDF 中提取文本并写入单个 CSV
- c++ - std::unitialized_copy 是否有未定义的行为?
- c# - 在 iOS 上使用 RestSharp 100% 丢包率
- scala - Scala:从 Spark 结构化流中读取 Kafka Avro 消息时出错
- php - 当我将数据库数据从 laravel 6 中的控制器传递到视图时,视图中的未定义变量