且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从可绘制的gif文件保存到手机SD?并在使用后将其从sd中删除

更新时间:2023-09-28 08:49:22

据我所知, Android上没有这样的本机函数(可能与专利问题有关)。您可以尝试此库以达到您的目的。通常,它的目的是创建动画 GIF ,但仅包含一张图片(框架)的文件也应该可以使用。

As far as i know there isn't such a native function on Android (maybe related to patent issues). You may try this library for your purpose. Usually its meant to create animated GIF but a file with just one picture (frame) should also work.

因此,假设您要处理非动画 GIF

So a complete example could be like this assuming you are dealing with a non animated GIF:

Bitmap someBitmap = [...];
File outputFile = new File("/sdcard/myNewGif.gif");
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(outputFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

if (fos != null) {
    AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder();
    gifEncoder.start(fos);
    gifEncoder.addFrame(someBitmap);
    gifEncoder.finish();
}

可以找到库作者的更多示例此处

More examples by the library author can be found here.