Dowload Media in Download directory in android app

91
April 27, 2022, at 1:00 PM

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

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.

Answer 2

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

You mean:

val file = File(Environment.getExternalStoragePyblicDirectory(Environment.DIRECTORY_DOWNLOADS),  fileName as String)
Rent Charter Buses Company
READ ALSO
Fixing header (navbar) in webview flutter

Fixing header (navbar) in webview flutter

I am currently working on a project where I need to convert a website to a mobile app using flutter I did it with the widget WEBVIEW but the header of the webview in my mobile is not fixed when i scroll the page the header (navBar) of my website scroll...

103
Transparent or Invisible

Transparent or Invisible

I'm trying to use an ImageView without any backgroundI know that I can use

125
how to use tap method instead of click method in appium?

how to use tap method instead of click method in appium?

I'm trying to automate an android test in appium and I have selected the webdriver as in wd/hub for this purposeHowever, the tap method there is virtually the click method as all the auto-recorded steps are generated as clicks instead of tap / touch

90