Binod Basnet
04/23/2024, 8:25 AMobject FileDownloaderUtils {
@JvmStatic
fun fileDownloader(
context: Context,
fileUrl: String,
outputFileName: String,
directoryName: String
) {
val modelsDir = File(context.filesDir, directoryName)
if (!modelsDir.exists()) {
modelsDir.mkdirs()
}
val outputFile = File(modelsDir, outputFileName)
if (!outputFile.exists()) {
val client = OkHttpClient()
val request = Request.Builder().url(fileUrl).build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
throw IOException("Failed to download file: ${response.message}")
} else {
// Handle the input stream
val inputStream = response.body?.byteStream()
val fileOutputStream =
FileOutputStream(File(context.filesDir, outputFileName))
inputStream?.use { input ->
fileOutputStream.use { output ->
input.copyTo(output)
}
}
}
}
})
}
}
}
Why this code doesnt execute after this line client.newCall(request).enqueue(object : Callback ) I have the binaryfile to download , url successfully download file when downloading from the browser but using okhttp it doesnt download, give me some suggestiongildor
04/23/2024, 8:30 AM