@@ -14,42 +14,36 @@ import java.net.URL
1414import kotlin.concurrent.thread
1515
1616// TODO 优化逻辑
17- fun ImageView.loadPicElement (pic : PicElement ) {
17+ fun ImageView.loadPicElement (pic : PicElement ) = apply {
1818 require(maxHeight != 0 )
1919 val cacheFile = context.externalCacheDir!! .child(" ${pic.md5HexStr} .jpg" )
2020 cacheFile.parentFile?.mkdirs()
2121 if (cacheFile.exists()) {
2222 Utils .log(" Load Image from disk ${cacheFile.path} " )
23- setImageBitmap( BitmapFactory .decodeFile( cacheFile.absolutePath) )
23+ bitmapDecodeFile( cacheFile)
2424 } else {
25- download(pic.getImageUrl()) { stream ->
26- if (stream == null ) {
25+ download(pic.getImageUrl(), cacheFile ) { succeed ->
26+ if (! succeed ) {
2727 val error = context.externalCacheDir!! .child(" error.jpg" )
2828 if (error.exists()) {
2929 bitmapDecodeFile(error)
3030 } else {
31- download(" https://i0.hdslb.com/bfs/new_dyn/e8907352f1c8be0ea696c1447723f6091769278028.png" ) { errorStream ->
32- val bitmap = bitmapDecodeStream(errorStream!! )
33- BufferedOutputStream (error.outputStream()).use {
34- bitmap?.compress(Bitmap .CompressFormat .JPEG , 100 , it)
31+ download(" https://i0.hdslb.com/bfs/new_dyn/e8907352f1c8be0ea696c1447723f6091769278028.png" , error) {
32+ if (it) {
33+ bitmapDecodeFile(error)
3534 }
3635 }
3736 }
3837 } else {
39- val bitmap = bitmapDecodeStream(stream)
40- BufferedOutputStream (cacheFile.outputStream()).use {
41- bitmap!! .compress(Bitmap .CompressFormat .JPEG , 100 , it)
42- }
38+ bitmapDecodeFile(cacheFile)
4339 }
4440 }
4541 }
4642}
4743
48- inline fun download (url : String , crossinline callback : (InputStream ? ) -> Unit ) {
44+ inline fun download (url : String , file : File , crossinline callback : (Boolean ) -> Unit ) {
4945 thread {
5046 var connection: HttpURLConnection ? = null
51- var inputStream: InputStream ? = null
52-
5347 try {
5448 connection = URL (url).openConnection() as HttpURLConnection
5549 connection.connectTimeout = 60_000 // 60秒超时
@@ -59,13 +53,20 @@ inline fun download(url: String, crossinline callback: (InputStream?) -> Unit) {
5953 Utils .log(" Download Image From: $url " )
6054 connection.connect()
6155 if (connection.responseCode == HttpURLConnection .HTTP_OK ) {
62- callback(connection.inputStream)
56+ if (! file.exists()) {
57+ file.createNewFile()
58+ }
59+ connection.inputStream.use { input ->
60+ file.outputStream().use { out ->
61+ input.copyTo(out )
62+ }
63+ }
6364 } else {
64- callback(null )
65+ callback(false )
6566 Utils .log(" Download Image Failed! $url " )
6667 }
6768 } catch (e: Exception ) {
68- callback(null )
69+ callback(false )
6970 e.printStackTrace()
7071 } finally {
7172 connection?.disconnect()
@@ -75,22 +76,19 @@ inline fun download(url: String, crossinline callback: (InputStream?) -> Unit) {
7576
7677fun ImageView.bitmapDecodeFile (file : File ): Bitmap ? =
7778 file.inputStream().use {
78- bitmapDecodeStream(it)
79- }
80- fun ImageView.bitmapDecodeStream (inputStream : InputStream ): Bitmap ? {
81- val rect = Rect ()
82- BitmapFactory .decodeStream(inputStream, rect, BitmapFactory .Options ().apply {
83- inJustDecodeBounds = true
84- })
85- val bitmap = BitmapFactory .decodeStream(inputStream, null , BitmapFactory .Options ().apply {
86- if (rect.height() > maxHeight) {
87- outHeight = maxHeight
88- outWidth = (rect.width().toFloat() / rect.height().toFloat() * maxHeight.toFloat()).toInt()
89- inSampleSize = rect.height() / maxHeight
79+ val rect = Rect ()
80+ BitmapFactory .decodeStream(it, rect, BitmapFactory .Options ().apply {
81+ inJustDecodeBounds = true
82+ })
83+ val bitmap = BitmapFactory .decodeStream(it, null , BitmapFactory .Options ().apply {
84+ if (rect.height() > maxHeight) {
85+ outHeight = maxHeight
86+ outWidth = (rect.width().toFloat() / rect.height().toFloat() * maxHeight.toFloat()).toInt()
87+ inSampleSize = rect.height() / maxHeight
88+ }
89+ })
90+ post {
91+ setImageBitmap(bitmap)
9092 }
91- })
92- post {
93- setImageBitmap(bitmap)
94- }
95- return bitmap
96- }
93+ bitmap
94+ }
0 commit comments