Search code examples
javaandroidgifanimated-gif

convert byte array into gif in java/android


In my android app i receive an array of bytes from a server and have to convert them into a .gif image and then place it in a view. I already know how to convert them into a still image using BitmapFactory, but the images always come out still. I am using gif-drawable as my container view for the .gif image. I have tested it with a local animated gif and it works great. How can i put the byte array into the gif-drawable view?

in the layout i have this:

<pl.droidsonroids.gif.GifImageView
            android:id="@+id/imgExerciseImage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="exercise image" />

this is a view similar to an ImageView that can display animated gifs

I Want to display in it an image that i receive from my firebase server. this is the code:

imageRef.getBytes(DatabaseConstants.ONE_MEGABYTE * 3).addOnSuccessListener(new OnSuccessListener<byte[]>() {
            @Override
            public void onSuccess(byte[] bytes) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                imgExerciseImage.setImageBitmap(bitmap);
                progressDialog.dismiss();
            }
        }

the byte[] is the animated gif. but in order to put it in my view i have to use the byte[] to form an image. in this example, im using BitmapFactory to create a bitmap. how can i create a gif instead of a bitmap?


Solution

  • it appears that gif-drawable comes with a GifDrawable object that can receive a byte array in it's constructor to create an animated gif that can then be placed in a GifImageView

    try {
        GifDrawable gif = new GifDrawable(bytes);
        imgExerciseImage.setImageDrawable(gif);
    } catch (IOException e) {
        e.printStackTrace();
    }