我正在尝试使用 Android Commit Content API 制作图像键盘。为了实现这一点,我需要能够在设备上存储 .png
和 .gif
文件并从中创建文件对象。
目前,我正在使用 Glide 库从互联网上获取图像。我可能获取的示例图像是 this 。 Glide 库为我提供了包含动画 GIF 的 drawables
。我需要能够将这些可绘制对象编码为 .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 文件。
最佳答案
这就是如何将 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()
}
关于java - 将动画 Drawable 保存为 GIF 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50660612/