Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
release stream add disconnect URLConnection, which may leading to the…
Browse files Browse the repository at this point in the history
… exhaustion of resources
  • Loading branch information
galwaychia committed Apr 24, 2019
1 parent 1591ed8 commit 2cea59d
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,25 @@ class SVGADynamicEntity {
fun setDynamicImage(url: String, forKey: String) {
val handler = android.os.Handler()
thread {
try {
(URL(url).openConnection() as? HttpURLConnection)?.let {
(URL(url).openConnection() as? HttpURLConnection)?.let {
try {
it.connectTimeout = 20 * 1000
it.requestMethod = "GET"
it.connect()
BitmapFactory.decodeStream(it.inputStream)?.let {
handler.post { setDynamicImage(it, forKey) }
it.inputStream.use { stream ->
BitmapFactory.decodeStream(stream)?.let {
handler.post { setDynamicImage(it, forKey) }
}
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
try {
it.disconnect()
} catch (disconnectException: Throwable) {
// ignored here
}
it.inputStream.close()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Expand Down

0 comments on commit 2cea59d

Please sign in to comment.