Skip to content

Instantly share code, notes, and snippets.

@weiancheng
Last active April 7, 2020 00:28
Show Gist options
  • Save weiancheng/aafdc03fe4bc0af931e934a71a023447 to your computer and use it in GitHub Desktop.
Save weiancheng/aafdc03fe4bc0af931e934a71a023447 to your computer and use it in GitHub Desktop.
[Android Download] #android
val file = File(filePath)
val fileOutputStream = FileOutputStream(file)
val inputStream = BufferedInputStream(URL(url).openStream())
var downloadSize = 0
val buffer = ByteArray(bufferSize)
var bufferLength: Int
while (true) {
bufferLength = inputStream.read(buffer)
if (bufferLength <= 0)
break
fileOutputStream.write(buffer, 0, bufferLength)
downloadSize += bufferLength
}
fileOutputStream.flush()
fileOutputStream.close()
inputStream.close()
if (downloadSize != totalSize) {
Log.e(tag, "file was not complete")
}
val file = File(filePath)
val fileOutputStream = FileOutputStream(file)
val inputStream = BufferedInputStream(URL(url).openStream())
var downloadSize = 0
val buffer = ByteArray(bufferSize)
var bufferLength: Int
while (true) {
bufferLength = inputStream.read(buffer)
if (bufferLength <= 0)
break
fileOutputStream.write(buffer, 0, bufferLength)
downloadSize += bufferLength
}
fileOutputStream.flush()
fileOutputStream.close()
inputStream.close()
if (downloadSize != totalSize) {
Log.e(tag, "file was not complete")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment