【问题标题】:OutOfMemoryError while trying to download large file using Retrofit @Streaming annotation尝试使用 Retrofit @Streaming 注释下载大文件时出现 OutOfMemoryError
【发布时间】:2019-12-20 21:58:15
【问题描述】:

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

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

这里是调用代码sn-p

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()

改装版 = 2.6.0

协程版本 = 1.3.0-M1

【问题讨论】:

    标签: java android kotlin retrofit kotlin-coroutines


    【解决方案1】:

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

    HttpLoggingInterceptor().apply {
       level = HttpLoggingInterceptor.Level.HEADERS
    })
    

    看起来很奇怪的错误修复,但它确实有效

    【讨论】:

      【解决方案2】:

      尝试使用下载管理器下载大文件

      您可以通过@CommonsWare 在这里找到完整的示例

      https://github.com/commonsguy/cw-android/tree/master/Internet/Download

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-14
        • 1970-01-01
        • 1970-01-01
        • 2014-09-07
        • 2019-02-15
        • 2013-08-11
        相关资源
        最近更新 更多