How to download file in Android using Retrofit library?

In kotlin, Do this:

In your service add method:

    @Streaming
    @GET
    suspend fun downloadFile(@Url fileUrl:String): Response<ResponseBody>

To call this method, from ViewModel:

viewModelScope.launch {
     val responseBody=yourServiceInstance.downloadFile(url).body()
     saveFile(responseBody,pathWhereYouWantToSaveFile)
}

To save file:

fun saveFile(body: ResponseBody?, pathWhereYouWantToSaveFile: String):String{
        if (body==null)
            return ""
        var input: InputStream? = null
        try {
            input = body.byteStream()
            //val file = File(getCacheDir(), "cacheFileAppeal.srl")
            val fos = FileOutputStream(pathWhereYouWantToSaveFile)
            fos.use { output ->
                val buffer = ByteArray(4 * 1024) // or other buffer size
                var read: Int
                while (input.read(buffer).also { read = it } != -1) {
                    output.write(buffer, 0, read)
                }
                output.flush()
            }
            return pathWhereYouWantToSaveFile
        }catch (e:Exception){
            Log.e("saveFile",e.toString())
        }
        finally {
            input?.close()
        }
        return ""
    }

Note:

  1. Make sure your refrofit client’s base url and the url passed to downloadFile makes valid file url:

Retrofit’s Base url + downloadFile’s method url = File url

  1. Here I am using suspend keyword before downloadFile to call this from ViewModel, I have used viewModelScope.launch {} you can use different coroutine scope according to your caller end.

  2. Now pathWhereYouWantToSaveFile, If you want to store file into project’s file directory, you can do this:

val fileName=url.substring(url.lastIndexOf("/")+1)
val pathWhereYouWantToSaveFile = myApplication.filesDir.absolutePath+fileName
  1. If you are storing the downloaded file under file or cache directory, you don’t need to acquire permission, otherwise for public storage, you know the process.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)