首页 > 解决方案 > 将文件上传到服务器时出现 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

如何解决这个问题?

标签: androidout-of-memorymultipartform-datamultipartsquare

解决方案


推荐阅读