Fuel android download big size files

357 Views Asked by At

I'm using Fuel in my Android code for downloading stuffs like PDF, Audio,Video and it works well, the problem arises when I wanna download big a file (50 MB and > file). I read about Fuel streamDestination but have no Idea how to solve this issue. Here is my code :

Fuel.download("https://$url")
                .fileDestination { response, url ->
                    File(path, fileName)
                }

                .timeout(5000)
                .response { request, response, result ->

                    result.fold(

                        success = {

                            progressDialog.dismissDialog()
                            listener.onDocDownloaded(fileName)
                        },
                        
                        failure = {

                            progressDialog.dismissDialog()
                                
                                   }
                            
                }
1

There are 1 best solutions below

0
On BEST ANSWER

I find solution thanks to Derk-Jan Karrenbeld. Here I just put working code :

        val outputStream = FileOutputStream(File(filePath))

        _downloadStatus.postValue(FileDownloadStatus.Downloading)

        Fuel.download(httpsedUrl)
            .streamDestination { response, _ -> Pair(outputStream, { response.body().toStream() }) }
            .fileDestination{response, request ->
                File(filePath)
            }
            .progress { readBytes, totalBytes ->
                val progress = readBytes.toFloat() / totalBytes.toFloat() * 100
                println("Bytes downloaded $readBytes / $totalBytes ($progress %)")
            }
            .response { result ->

                result.fold(
                        success = {
                            _downloadStatus.postValue(FileDownloadStatus.Downloaded)
                        },
                        failure = {
                            _downloadStatus.postValue(it.message?.let { it1 -> FileDownloadStatus.Failed(it1) })
                        }
                )
            }