Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top