将动画Drawable保存到GIF文件?

时间:2018-06-02 19:51:01

标签: java kotlin gif android-drawable android-glide

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

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

我该怎么做?

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

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

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

1 个答案:

答案 0 :(得分:1)

这是将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()
}
相关问题