I am a newbie to the Android platform, I am creating an android application which downloads files from the internet: I found some examples on the internet regarding download file but all of them are download file from url as
http/https:sample_address/file_to_download.xyz
. But my application need to download file from url as *
http/https:sample_address/file_to_download.xyz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20221025%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221025T093017Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3D%2220_2022-24-10.apk%22&X-Amz-Signature=4968bbf4093b5d1fa6f0368ac1cfa445480bc301b132e740a7cf38fe81d9d2b5
Below are lines of code that I use to download files but it doesn't work
private fun downloadUpdate(apkPath: String, versionCode: Int) =
serviceScope.launch {
try {
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
val apkFileName = "${versionCode}.apk"
val appDataRoot = File(AppConstants.EXTERNAL_STORAGE_PATCH)
//create a new file, specifying the path, and the filename
//which we want to save the file as.
val file = File(appDataRoot, apkFileName)
GLog.d(UpdateSoftwareViewModel.TAG, "AppUpdate apkFileName =${apkFileName}")
GLog.d(UpdateSoftwareViewModel.TAG, "AppUpdate apkFileName exist =${file.exists()}")
var updatedApkExist = file.exists()
if (!updatedApkExist) {
//create the new connection
/*https:sample_address/file_to_download.xyz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20221025%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221025T093017Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3D%2220_2022-24-10.apk%22&X-Amz-Signature=4968bbf4093b5d1fa6f0368ac1cfa445480bc301b132e740a7cf38fe81d9d2b5*/
val pathSplit = apkPath.split("?")
val connectionParams = pathSplit[1].split("&")
val urlConnection = URL(pathSplit[0]).openConnection() as HttpURLConnection
connectionParams.forEach {
val param = it.split("=")
GLog.d(TAG,"setRequestProperty ${param[0]} = ${param[1]}")
urlConnection.setRequestProperty(param[0],param[1])
}
//set up some things on the connection
urlConnection.requestMethod = "GET"
urlConnection.doOutput = true
//and connect!
urlConnection.connect()
//this will be used to write the downloaded data into the file we created
val fileOutput = FileOutputStream(file)
//this will be used in reading the data from the internet
val inputStream = urlConnection.inputStream //Exception HERE: java.io.FileNotFoundException: https:sample_address/file_to_download.xyz
//this is the total size of the file
val totalSize: Int = urlConnection.contentLength
//variable to store total downloaded bytes
var downloadedSize = 0
//create a buffer...
val buffer = ByteArray(1024)
var bufferLength: Int //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while (inputStream.read(buffer).also { bufferLength = it } > 0 && isActive) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength)
//add up the size so we know how much is downloaded
downloadedSize += bufferLength
//Send message update progress
val message = Message()
message.what = AppConstants.DOWN_LOAD_PROGRESS
message.arg1 = downloadedSize / totalSize
downloadHandlerList.forEach {
it.sendMessage(message)
}
}
//close the output stream when done
fileOutput.close()
updatedApkExist = downloadedSize == totalSize
}
if (updatedApkExist) {
GLog.d(VerifyUserViewModel.TAG, "AppUpdate do install")
installApplication(this@UpdateSoftwareService, file.absolutePath)
}
//Stop service when the download task is done
stopSelf()
} catch (e: MalformedURLException) {
e.printStackTrace()
GLog.d(UpdateSoftwareViewModel.TAG, "MalformedURLException exception ${e.message}")
} catch (e: IOException) {
e.printStackTrace()
GLog.d(UpdateSoftwareViewModel.TAG, "IOException exception ${e.message}")
}
}
I don't want to use DownloadManager because I want to publish the progress of the download process.
Could you help me to download the file from the internet in url format as
https:sample_address/file_to_download.xyz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20221025%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221025T093017Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3D%2220_2022-24-10.apk%22&X-Amz-Signature=4968bbf4093b5d1fa6f0368ac1cfa445480bc301b132e740a7cf38fe81d9d2b5