Search code examples
androidmovieanimated-gif

Failed to decode some animated GIF files using Movie on Android 2.3.x or lower


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.sstatic.net/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.


Solution

  • 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