0

I am trying to download an mp3 file from an http link and save the file to local storage. All the code I have tried saves a corrupt file that is slightly twice as large as it should be. File should be 1,204,787 File saved is 2,478,272

The file I am trying to download is: rise-stage.bioinf.unc.edu/cue_audio/sampleaudio.mp3 –

It plays fine when downloaded manually.

fun downloadFilea(url:String , localFileName:String)
{
    val request: Request = Request.Builder()
        .url(url)
        .build()

    client.newCall(request).enqueue(object : Callback {

        override fun onFailure(call: Call, e: IOException) {
            println("error"+e.toString())
        }

        @Throws(IOException::class)
        override fun onResponse(call: Call, response: Response) {
            if (!response.isSuccessful) throw IOException("Unexpected code $response")

            var uri = dataMgr.getLocalURI(localFileName)
            var file = File(uri)
            val body = response.body

            val contentLength = body!!.contentLength()
            val source = body.source()
            val DOWNLOAD_CHUNK_SIZE:Long = 2048
            val sink: BufferedSink = file.sink().buffer()

            var totalRead: Long = 0
            var read: Long = 0
            while (source.read(sink.buffer(), DOWNLOAD_CHUNK_SIZE).also {
                    read = it
                } != -1L) {
                totalRead += read
                val progress = (totalRead * 100 / contentLength).toInt()
            }
            sink.writeAll(source)
            sink.flush()
            sink.close()

            Log.d(logTag, "downloaded file")
        }
    })
}
user3561494
  • 2,164
  • 1
  • 20
  • 33
  • The file I am trying to download is: https://rise-stage.bioinf.unc.edu/cue_audio/sampleaudio.mp3 – user3561494 Jun 28 '20 at 16:21
  • [This approach](https://stackoverflow.com/a/29012988/115145) is less code, more efficient, and works for me. – CommonsWare Jun 28 '20 at 16:24
  • @CommonsWare Some of that code is deprecated, and also produces the same problem. – user3561494 Jun 29 '20 at 12:42
  • You can see it in use [here](https://gitlab.com/commonsguy/cw-jetpack-kotlin/-/blob/v1.0/DownloadWork/src/main/java/com/commonsware/jetpack/work/download/DownloadWorker.kt), and there are no deprecation warnings that I see. Even when I update it to the latest OkHttp (4.7.2), there are no deprecation warnings. – CommonsWare Jun 29 '20 at 12:58
  • It seems that this code works for text files but when it's an mp3 file, it produces a corrupt file that is twice as large as it should be. – user3561494 Aug 01 '20 at 21:01
  • The code in question [was endorsed by an Okio developer](https://stackoverflow.com/questions/25893030/download-binary-file-from-okhttp/29012988#comment46367343_29012988). And the sample that I linked to downloads a PDF, which is not a text file. [Here](https://gitlab.com/commonsguy/cw-android-q/-/blob/vFINAL/ConferenceVideos/src/main/java/com/commonsware/android/conferencevideos/VideoRepository.kt) I download a video, which is also not a text file. – CommonsWare Aug 01 '20 at 21:06

0 Answers0