2

I tried using the following snippet code to decode the animated gif file with the Movie class.

            URL url;
            InputStream is = null;
            BufferedInputStream bis = null;
                url = new URL("http://emos.plurk.com/aeddddbbf63e980128ab7b3c1fca2798_w48_h48.gif");
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

                is = httpURLConnection.getInputStream();
                bis = new BufferedInputStream(is);
                byte[] array = streamToBytes(bis);
                mGifMovie = Movie.decodeByteArray(array, 0, array.length);

and draw the Movie in the overrided onDraw method in an extended ImageView like this:

if (mGifMovie != null) {
        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) { // first time
            mMovieStart = now;
        }
        if (mGifMovie != null) {
            int dur = mGifMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }
            int relTime = (int) ((now - mMovieStart) % dur);
            mGifMovie.setTime(relTime);
            mGifMovie.draw(canvas, 0, 0);
            invalidate();
        }
    }

Some animated gif works, but some others like this one are not. It came out with some frames are failed to be decode. Here is the screenshot: https://i.stack.imgur.com/le4bC.png (original image: http://emos.plurk.com/aeddddbbf63e980128ab7b3c1fca2798_w48_h48.gif)

I tested with some other apps on the market. They decode the animated gif successfully. How should I make it correct?

EDIT: I would like to decode the animated Gif correctly on my 2.3.x devices like those apps on the market.

shiami
  • 7,174
  • 16
  • 53
  • 68
  • Since Android is quite limited with decoding Gif, you could try to open the Animated Gif in Photoshop, and store it with different settings. At least, if you are able to upload your "new" gif to the server. My guess is, that when the gif is saved slightly different, it might be able to playback right... Sorry for this lousy comment :) – Entreco Nov 15 '11 at 14:00
  • Unfortunately, I have to decode various animated GIF files on the website. So there is not possible to deal with them one by one. – shiami Nov 17 '11 at 03:19

2 Answers2

3

What version of Android are you running on? Support for animated GIF in the Movie class was quite limited before this commit is merged into the AOSP code around September 2010, which I believe is after the first Gingerbread release (API level 9).

Most likely the other apps that works have their own internal GIF decoding code and did not use the Movie class.

UPDATE: I went ahead and tested the code on the emulator for android-10 (GB MR2) and android-14 (ICS). On the android-10 emulator, the image is still improperly decoded, but it works fine on the android-14 emulator.

Joe
  • 14,039
  • 2
  • 39
  • 49
  • It fails on my device v2.3.4. – shiami Nov 11 '11 at 06:41
  • Ah, I wasn't sure which Android version that commit was merged. Would it be possible for you to try running your app in the emulator? Try it with an AVD created with "android-9" and "android-10" to see if it behaves differently. Anyway, my point is that you might not be able to use the Movie class reliably for GIF decoding. – Joe Nov 11 '11 at 07:12
  • So is there a better solution? – shiami Nov 11 '11 at 08:59
  • I tested and works on 3.0+ devices. But not works on 2.3.x devices. – shiami Nov 17 '11 at 03:16
0

I turned out to use custom gif decoder to run on under 3.0 devices. Here is the java sample code: http://www.java2s.com/Code/Java/2D-Graphics-GUI/DecodesaGIFfileintooneormoreframes.htm

shiami
  • 7,174
  • 16
  • 53
  • 68
  • 1
    not all gif images are supported using API.. :S check more options here (there are three parts of this tutorial).. http://droid-blog.net/2011/10/15/tutorial-how-to-play-animated-gifs-in-android-%e2%80%93-part-2/ – Ewoks Oct 26 '12 at 08:33
  • How did you succeed to turn the BufferedImage frame into something usabke in Android (Bitmap or Drawable)? – Waza_Be May 25 '13 at 18:21
  • @Waza_Be: You can take a look the AnimationDrawable source code. – shiami May 27 '13 at 07:29