ImageIcon (GIF) To Byte Array

375 Views Asked by At

I'm currently working on a bot project for a Discord server with different activities. Now, I want the bot to be able to send gifs, but as far as I have explored in the Discord4j API you need to send it as an InputStream.

ImageIO.write method doesn't support gifs but ImageIcon does and that's why I can't use the ImageIO.write to write to the ByteArrayOutputStream. Do I have to stick to sending static photos or is there another way? Or is the solution to use another API?

Here's how the method looks right now:

public final static void sendPicture(final BufferedImage image, final MessageChannel channel) throws IOException
{
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ImageIO.write(image, "gif", outputStream);
    final InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

    System.out.println("Sending picture...");
    channel.createMessage(messageCreateSpec -> {
        messageCreateSpec.addFile("output.gif", inputStream);
    }).block();

    outputStream.close();
    inputStream.close();
}
0

There are 0 best solutions below