For anyone downloading files in compose multiplatform this works too for android client //actual suspend fun downloadLoanStatement(
accountNumber: String,
url: String,
authToken: String?
) {
val request = Request.Builder()
.url(url)
.header(“Authorization”, “Bearer $authToken”)
.build()
val client = OkHttpClient()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e(“DownloadFile”, “Error downloading file: ${e.message}“)
}
override fun onResponse(call: Call, response: Response) {
response.body?.let { responseBody ->
try {
val contentDisposition = response.header(“Content-Disposition”)
val fileName =
extractFileName(contentDisposition)
val directory = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
“downloaded”
)
if (!directory.exists()) {
directory.mkdirs()
}
val file = File(directory, fileName)
val outputStream = FileOutputStream(file)
responseBody.byteStream().use { input ->
outputStream.use { output ->
input.copyTo(output)
}
}
Handler(Looper.getMainLooper()).post {
Toast.makeText(
context,
“Download complete: $fileName”,
Toast.LENGTH_LONG
).show()
}
showDownloadCompleteNotification(context, file)
} catch (e: IOException) {
Log.e(“DownloadFile”, “Error downloading file: ${e.message}“)
}
} ?: Log.e(“DownloadFile”, “Response body is null”)
}
})
}
}