Skip to content

Commit

Permalink
better handle ImageBitmapDecoder errors
Browse files Browse the repository at this point in the history
  • Loading branch information
luca992 committed Dec 7, 2023
1 parent 83248a4 commit b6a1c94
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.kamel.image.decoder

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
Expand All @@ -18,7 +17,8 @@ internal actual object ImageBitmapDecoder : Decoder<ImageBitmap> {

override suspend fun decode(channel: ByteReadChannel, resourceConfig: ResourceConfig): ImageBitmap {
val bytes = channel.toByteArray()
val bitmap = BitmapFactory.decodeByteArray(bytes, Offset, bytes.size) as Bitmap
val bitmap = BitmapFactory.decodeByteArray(bytes, Offset, bytes.size)
?: throw IllegalArgumentException("Failed to decode ${bytes.size} bytes to a bitmap. Decoded bytes:\n${bytes.decodeToString()}\n")
return bitmap.asImageBitmap()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ internal actual object ImageBitmapDecoder : Decoder<ImageBitmap> {
override suspend fun decode(
channel: ByteReadChannel,
resourceConfig: ResourceConfig
): ImageBitmap = Image.makeFromEncoded(channel.toByteArray()).toComposeImageBitmap()
): ImageBitmap {
val bytes = channel.toByteArray()
return try {
Image.makeFromEncoded(bytes).toComposeImageBitmap()
} catch (t: Throwable) {
throw throw IllegalArgumentException("Failed to decode ${bytes.size} bytes to a bitmap. Decoded bytes:\n${bytes.decodeToString()}\n")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ internal actual object ImageBitmapDecoder : Decoder<ImageBitmap> {
override val outputKClass: KClass<ImageBitmap> = ImageBitmap::class

override suspend fun decode(
channel: ByteReadChannel,
resourceConfig: ResourceConfig
): ImageBitmap = Image.makeFromEncoded(channel.toByteArray()).toComposeImageBitmap()
channel: ByteReadChannel, resourceConfig: ResourceConfig
): ImageBitmap {
val bytes = channel.toByteArray()
return try {
Image.makeFromEncoded(bytes).toComposeImageBitmap()
} catch (t: Throwable) {
throw throw IllegalArgumentException("Failed to decode ${bytes.size} bytes to a bitmap. Decoded bytes:\n${bytes.decodeToString()}\n")
}
}

}

0 comments on commit b6a1c94

Please sign in to comment.