简体   繁体   中英

Fuel android download big size files

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM