将动画可绘制对象保存到 GIF 文件?

Save Animated Drawable to GIF File?

我正在尝试使用 Android Commit Content API 制作图像键盘。为了实现这一点,我需要能够在设备上存储 .png.gif 文件并从中创建文件对象。

目前,我正在使用 Glide 库从互联网上获取图像。我可能会获取的示例图像是 this。 Glide 库为我提供了包含动画 GIF 的 drawables。我需要能够将这些可绘制对象编码为 .gif 文件,这些文件可以作为文件保存在用户的 phone 上。

我该怎么做?

我查看了现有的 post,例如:How to convert a Drawable to a Bitmap?,但它们没有帮助,因为 Bitmap 对象无法存储动画 GIF。

这个 post: saving gif file from drawable into phone sd? and remove it from sd after usage 似乎也相关,但是,它没有解释如何将动画 Drawable 对象转换为 Bitmap 对象列表。

此外,它没有说明如何将一系列 Bitmap 对象保存为一个 GIF 文件。

这是将 Glide 库中的 GifDrawable 另存为文件的方法。

private fun gifDrawableToFile(gifDrawable: GifDrawable, gifFile: File) {
    val byteBuffer = gifDrawable.buffer
    val output = FileOutputStream(gifFile)
    val bytes = ByteArray(byteBuffer.capacity())
    (byteBuffer.duplicate().clear() as ByteBuffer).get(bytes)
    output.write(bytes, 0, bytes.size)
    output.close()
}