Welcome to FutureAppLaboratory

v=(*^ワ^*)=v

GIF View for Android

| Comments

Movie view does not work sometimes

There are several ways to decode and show GIF format image on android.
I tried using framework’s Movie class to decode GIF, but it crashes on device which runs 4.1 or later.
Don’t know why that occurs but I need to show animated GIF on all OS version.
So I write a custom view to do that, and in a simple way.

Source can be found on Github.

Something Wired

GIF’s document says that it is encoded in little-endianGIF Specification.
But sometimes the HEADER part is in big-endian, like this.

Image 01

The first 6 bytes 47 49 46 38 39 61 is GIF89a, in big-endian. But sometimes in little-endian.(Not confirmed but I found some open source libraries read them by little-endian).
Note that, the following image size sector 7c02 fa00 (width: 636, height: 250) is little-endian.
So I should read bytes first in BIG_ENDIAN, then turn byte order into LITTLE_ENDIAN.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * Reads GIF file header information.
 */
protected void readHeader() {
    String id = "";
    rawData.order(ByteOrder.BIG_ENDIAN);
    for (int i = 0; i < 6; i++) {
        id += (char) read();
    }
    rawData.order(ByteOrder.LITTLE_ENDIAN);
    if (!id.startsWith("GIF")) {
        status = STATUS_FORMAT_ERROR;
        return;
    }
    readLSD();
    if (gctFlag && !err()) {
        gct = readColorTable(gctSize);
        bgColor = gct[bgIndex];
    }
}

Comments