简体   繁体   English

是否将动画Drawable保存到GIF文件?

[英]Save Animated Drawable to GIF File?

I am trying to make an image keyboard with the Android Commit Content API . 我正在尝试使用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. 为了实现这一点,我需要能够将.png.gif文件存储在设备上,并从它们创建文件对象。

Currently, I am using the Glide library to fetch images from the internet. 目前,我正在使用Glide库从互联网上获取图像。 An example image I might fetch is this . 我可能会获取的示例图像是this The Glide library provides me with drawables that contain animated GIFs. Glide库为我提供了包含动画GIF的drawables I need to be able to encode these drawables into .gif files that can be saved as files on the user's phone. 我需要能够将这些可绘制对象编码为.gif文件,然后将其另存为用户手机上的文件。

How can I do this? 我怎样才能做到这一点?

I've looked at existing posts, such as this: How to convert a Drawable to a Bitmap? 我查看了现有的帖子,例如: 如何将Drawable转换为Bitmap? , but they aren't helpful because the Bitmap object cannot store animated GIFs. ,但它们无济于事,因为Bitmap对象无法存储动画GIF。

This post: saving gif file from drawable into phone sd? 这篇文章: 将gif文件从可绘制文件保存到手机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. 并在使用后将其从sd中删除似乎也很相关,但是,它没有说明如何将动画的Drawable对象变成Bitmap对象列表。

Also, it doesn't explain how to save a series of Bitmap objects as 1 GIF file. 另外,它也没有说明如何将一系列Bitmap对象另存为1个GIF文件。

This is how you can save a GifDrawable from the Glide library as a file. 这样可以将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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM