saving gif file from drawable into phone sd? and remove it from sd after usage

£可爱£侵袭症+ 提交于 2019-12-18 09:43:23

问题


I have been searching on how to save file from Drawable into sd card OR phone's internal storage. All examples such as this one showed only for PNG type, I want to get GIF file from my drawable and save it into phone sd, and later remove it from sd after usage.

Anybody please help with the code? I appreciate it!


回答1:


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.

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.



来源:https://stackoverflow.com/questions/23126188/saving-gif-file-from-drawable-into-phone-sd-and-remove-it-from-sd-after-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!