Home > Mobile >  Dowload Media in Download directory in android app
Dowload Media in Download directory in android app

Time:02-07

Am trying to download media file from server to android default Downloads directory,but on storing the media in downloads i got exception java.io.FileNotFoundException: Download/01-21-2022 04:15.jpeg (No such file or directory)

here is my code

val fileName = message.attributes.jsonObject!!.get("fileName")
val contentLength = response.body()!!.contentLength()
val file = File(Environment.DIRECTORY_DOWNLOADS, fileName as String)
responseBody.byteStream().apply {
    file.outputStream().use { fileOut ->
    var bytesCopied = 0
    val buffer = ByteArray(8*1024)
    var bytes = read(buffer)
    while (bytes >= 0) {
     fileOut.write(buffer, 0, bytes)
     bytesCopied  = bytes
     bytes = read(buffer)
     reportStatus(((bytesCopied * 100) / contentLength).toInt())
    }
   }
  }

CodePudding user response:

You have all to fix this. "No such file or directory". Create file before usage. Try without apply, with verification that file was created. Be sure, that you have access to this directory.

CodePudding user response:

val file = File(Environment.DIRECTORY_DOWNLOADS, fileName as String)

You mean:

val file = File(Environment.getExternalStoragePyblicDirectory(Environment.DIRECTORY_DOWNLOADS),  fileName as String)
  •  Tags:  
  • Related