问题
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: http://i.imgur.com/HsxcP.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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/8088865/failed-to-decode-some-animated-gif-files-using-movie-on-android-2-3-x-or-lower