Issue
I am trying to make an image keyboard with the Android Commit Content API. In order to implement this, I need be able to store .png
and .gif
files on the device and create file objects from them.
Currently, I am using the Glide library to fetch images from the internet. An example image I might fetch is this. The Glide library provides me with drawables
that contain animated GIFs. I need to be able to encode these drawables into .gif
files that can be saved as files on the user's phone.
How can I do this?
I've looked at existing posts, such as this: How to convert a Drawable to a Bitmap?, but they aren't helpful because the Bitmap
object cannot store animated GIFs.
This post: saving gif file from drawable into phone sd? and remove it from sd after usage also seems relevant, but, it doesn't explain how to turn an animated Drawable
object into a list of Bitmap
objects.
Also, it doesn't explain how to save a series of Bitmap
objects as 1 GIF file.
Solution
This is how you can save a GifDrawable
from the Glide library as a file.
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()
}
Answered By - Foobar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.