代码之家  ›  专栏  ›  技术社区  ›  Foobar

是否将可绘制的动画保存到gif文件?

  •  0
  • Foobar  · 技术社区  · 7 年前

    我想用安卓做一个图像键盘 Commit Content API . 为了实现这个,我需要能够存储 .png .gif 设备上的文件并从中创建文件对象。

    目前,我正在使用glide库从互联网上获取图像。我可能会得到的一个例子是 this . Glide图书馆为我提供 drawables 包含动画gif的。我需要能把这些可提取的编码成 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 对象的列表 位图 物体。

    而且,它也不能解释如何保存一系列 位图 对象为1个gif文件。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Foobar    6 年前

    这就是你如何拯救 GifDrawable 作为文件从Glide库中获取。

    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()
    }