47

I am looking for a simple way to create an animated GIF in a native Android application. The source files should be JPEG (from camera or what ever) and the output should be saved as GIF on the device.

I do not want to know how to play animations or animated GIF files.

To be clear: I want to know how to put single images frame by frame into a "movie" and then save it as a .gif file.

e.g. This App can do want I want to do.

4
  • 2
    Here is my standalone animated GIF writer for Android. It's quick and you can choose to dither for better quality. Commented Oct 25, 2015 at 0:03
  • @dragon66 Can you please add a release tag so we can use JitPack? Commented Apr 9, 2020 at 12:35
  • @TheRealChx101 there is no release yet Commented Apr 9, 2020 at 18:31
  • @dragon66 Make one then :) Commented Apr 9, 2020 at 18:32

6 Answers 6

60

See this solution.

https://github.com/nbadal/android-gif-encoder

It's an Android version of this post.

http://www.jappit.com/blog/2008/12/04/j2me-animated-gif-encoder/

To use this class, here is an example helper method to generate GIF byte array. Note here the getBitmapArray() function is a method to return all the Bitmap files in an image adapter at once. So the input is all the Bitmap files in one adapter, the output is a byte array which you can write to the file.

public byte[] generateGIF() { ArrayList<Bitmap> bitmaps = adapter.getBitmapArray(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); AnimatedGifEncoder encoder = new AnimatedGifEncoder(); encoder.start(bos); for (Bitmap bitmap : bitmaps) { encoder.addFrame(bitmap); } encoder.finish(); return bos.toByteArray(); } 

To use this function, do the following then you can save the file into SDcard.

FileOutputStream outStream = null; try{ outStream = new FileOutputStream("/sdcard/generate_gif/test.gif"); outStream.write(generateGIF()); outStream.close(); }catch(Exception e){ e.printStackTrace(); } 
Sign up to request clarification or add additional context in comments.

13 Comments

Does this compute a gif with 8-bit or 24-bit colors?
encoder.addFrame(bitmap); but addFrame looking for image path right ?The method addFrame(Image) in the type AnimatedGifEncoder is not applicable for the arguments (Bitmap)
@Achayan it should take Bitmap as argument.
There is an image processing library, akin to square.github.io/picasso which uses the very same AnimatedGifEncoder class mentioned by Lifelogger- bumptech.github.io/glide/javadocs/latest/index.html, github.com/bumptech/glide
this is very slow for me. about 6 seconds for a frame on my nexus 5x. each frame is 1800x1000
|
7
+50

This might help you. It's a class which is used to generate gif files.

http://elliot.kroo.net/software/java/GifSequenceWriter/

6 Comments

There is a way of doing what you want. First make a byte array of images. Then add header information of gif file then save this file.
Do you have an example? I managed it to save a file from byte array, but I don't know the header information.
w3.org/Graphics/GIF/spec-gif89a.txt FOLLOW THIS. It consists all the header info a gif required. Or wiki git format and find all the header byte info. Then just add those values in your byte array.
This is not the perfect answer because I haven't a working solution jet, but it is the closest answer to get the bounty...
Thanks :) I'll try to do the same thing. The ans of this question by user3047840 is just perfect.
|
6

It did very well for me, It create file fast and good quality images

//True for dither. Will need more memory and CPU AnimatedGIFWriter writer = new AnimatedGIFWriter(true); OutputStream os = new FileOutputStream("animated.gif"); Bitmap bitmap; // Grab the Bitmap whatever way you can // Use -1 for both logical screen width and height to use the first frame dimension writer.prepareForWrite(os, -1, -1) writer.writeFrame(os, bitmap); // Keep adding frame here writer.finishWrite(os); // And you are done!!! 

https://github.com/dragon66/android-gif-animated-writer

3 Comments

One thing about this encoder is it can write the animated gif frame by frame to reduce memory consumption
is it going to save the gif file too ?
Yes, it saved the gif with name animated.gif
4

If you only want to display these bitmaps like an animated gif, you can create an AnimatedDrawable using this code:

AnimationDrawable animation = new AnimationDrawable(); animation.addFrame(getResources().getDrawable(R.drawable.image1), 10); animation.addFrame(getResources().getDrawable(R.drawable.image2), 50); animation.addFrame(getResources().getDrawable(R.drawable.image3), 30); animation.setOneShot(false); ImageView imageAnim = (ImageView) findViewById(R.id.imageView); imageAnim.setImageDrawable(animation); // start the animation! animation.start(); 

Comments

1

Solution 1

Have you seen this article? ..... it's with source code..

As I know, I think android won't play gif animation image. You should use webview that will play gif animation..

http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/

and if you can..

http://androidosbeginning.blogspot.in/2010/09/gif-animation-in-android.html

Solution 2

Android provides Drawable Animation.

2 Comments

Hm... Thanks, but this is just how to play GIFs. But I want to create a new GIF file out of files from my device. I want to know how to but single images frame by frame into a "movie" and then save it as a .gif file. I did not find something like that in your links. Or am I blind? :)
You should've put this explanation in an EDIT to the original question ;)
0

Gif is basically a set of images with a constant time delay in frames. You can not directly play gif in android. I've worked lot on playing gif animation in android.

  • When playing gif we also handle all calls of playing frames by our self. So this is not a best approach

  • If you have set of images just play them by a delay in frames. That would be the all you want.

  • You can not make gif in application by native code as well. There are some drawbacks as well for using gif format. Which I had faced in my game.

  • If you have set of drawables you can use AnimationDrawable for showing aanimation like gif as well. It can also set the any View.

I had also made a custom view to play gif animations. First I load gif and convert it into InputStream then I pass it to my custom view class to play the gif.

 public class GifWebView extends View { private Movie mMovie; InputStream mStream; long mMoviestart; private boolean play; public GifWebView(Context context, InputStream stream) { super(context); mStream = stream; setPlay(true); mMovie = Movie.decodeStream(mStream); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.TRANSPARENT); super.onDraw(canvas); final long now = SystemClock.uptimeMillis(); if (mMoviestart == 0) { mMoviestart = now; } final int relTime = (int) ((now - mMoviestart) % mMovie.duration()); mMovie.setTime(relTime); mMovie.draw(canvas, 20, 20); if (play) { Log.i("reltime", "" + relTime + ",duration:" + mMovie.duration()); this.invalidate(); } } @Override public boolean onTouchEvent(android.view.MotionEvent event) { return true; }; public boolean isPlay() { return play; } 

1 Comment

Thank you, but I didn't ask how to play GIF files. I asked how to create GIF files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.