public fun Bitmap.toImageResource(): ImageResource { val rgb565Bitmap = if (config == Bitmap.Config.RGB_565) { this } else { copy(Bitmap.Config.RGB_565, false) } val byteBuffer = ByteBuffer.allocate(rgb565Bitmap.byteCount) rgb565Bitmap.copyPixelsToBuffer(byteBuffer) val bytes: ByteArray = byteBuffer.array() return ImageResource.Builder().setInlineResource( ResourceBuilders.InlineImageResource.Builder() .setData(bytes) .setWidthPx(rgb565Bitmap.width) .setHeightPx(rgb565Bitmap.height) .setFormat(ResourceBuilders.IMAGE_FORMAT_RGB_565) .build() ) .build() }
Convert a bitmap to a ImageResource.