Skip to content

Commit 65c08d7

Browse files
authored
Refactor resizeImage function for better resource handling
Refactor image resizing to use input stream safely and improve bitmap scaling.
1 parent 2c1a094 commit 65c08d7

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

  • llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/ETImage.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,21 @@ class ETImage(
8787
private fun resizeImage(uri: Uri, sideSize: Int): Bitmap? {
8888
val inputStream = contentResolver.openInputStream(uri)
8989
if (inputStream == null) {
90-
ETLogging.getInstance().log("Unable to resize image, input streams is null")
90+
ETLogging.getInstance().log("Unable to resize image, input stream is null")
9191
return null
9292
}
93-
val bitmap = BitmapFactory.decodeStream(inputStream)
93+
val bitmap = inputStream.use {
94+
BitmapFactory.decodeStream(it)
95+
}
9496
if (bitmap == null) {
9597
ETLogging.getInstance().log("Unable to resize image, bitmap during decode stream is null")
9698
return null
9799
}
98-
99-
return Bitmap.createScaledBitmap(bitmap, sideSize, sideSize, false)
100+
val scaled = Bitmap.createScaledBitmap(bitmap, sideSize, sideSize, true) // ← bilinear
101+
if (scaled !== bitmap) {
102+
bitmap.recycle()
103+
}
104+
return scaled
100105
}
106+
101107
}

0 commit comments

Comments
 (0)