尝试使用 Retrofit @Streaming 注释下载大文件时出现 OutOfMemoryError

jem*_*ili 6 java android kotlin retrofit kotlin-coroutines

我正在使用改造客户端下载文件,但是当存在大文件(200 MB)时它会抛出错误java.lang.OutOfMemoryError:@Streaming也有注释,这是我的下载服务方法

@Streaming
@GET("{path}")
suspend fun downloadFile(@Path("path") path: String): Response<ResponseBody>`
Run Code Online (Sandbox Code Playgroud)

这是调用代码片段

suspend fun downloadFile(remotePath: String): FileDownloadResponse {
  try {
    val response = api.downloadFile(remotePath)
    if (response.isSuccessful) {
       FileDownloadResponse.Success(response.body()!!)
    } else {
      FileDownloadResponse.Fail()
    }
   } catch (e: Exception) {
     e.printStakTrace()
     FileDownloadResponse.Fail(throwable = e)

   }

}

val response = remoteRepositroy.downloadFile(remotePath)
val writeResult = response.body.writeResponseBodyToDisk()
Run Code Online (Sandbox Code Playgroud)

改装版=2.6.0

协程版本=1.3.0-M1

jem*_*ili 7

HttpLogingInterceptor我通过将日志级别从BODY更改为 来修复它HEADERS

HttpLoggingInterceptor().apply {
   level = HttpLoggingInterceptor.Level.HEADERS
})
Run Code Online (Sandbox Code Playgroud)

似乎奇怪的错误修复,但它有效