6

I'm downloading files using retrofit client, but when there is large file (200 MB) it throws java.lang.OutOfMemoryError: I have @Streaming annotation also and this is my download service method

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

Here is invoke code snippet

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

Retrofit version = 2.6.0

Coroutine version = 1.3.0-M1

Jemo Mgebrishvili
  • 5,187
  • 7
  • 38
  • 63

2 Answers2

6

I have fixed it by changing HttpLogingInterceptor log level from BODY to HEADERS

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

Seems strange bug fix, but it works

Jemo Mgebrishvili
  • 5,187
  • 7
  • 38
  • 63
0

Try using download manager for downloading large files

You can find the complete sample here by @CommonsWare

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

Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42