-1

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?

BelgishChoko
  • 393
  • 2
  • 12
  • Why not convert the input stream to an image? Plenty of support for that. No need to read the entire thing into a byte array yourself. – user207421 Oct 20 '16 at 10:27
  • Because I'm using firebase and I can't get anything except a byte array – BelgishChoko Oct 20 '16 at 10:28
  • What de you mean by that? You will receive a stream of bytes. Not a byte array. It is up to you what you do with the bytes. If you put them in an array yes then you have a byte array. – greenapps Oct 20 '16 at 11:33
  • Further it it unclear what the bytes would represent. What do they contain if you have to convert them to gif? A jpg? A png? A bmp? Isnt it already a gif image? – greenapps Oct 20 '16 at 11:34
  • To use firebase you use a function to read the info from the server and then you receive the data in a callback that has a byte array parameter. I don't have any interaction with the inputstream – BelgishChoko Oct 20 '16 at 11:36
  • The bytes represent a gif. How would I insert them into a view? – BelgishChoko Oct 20 '16 at 11:38
  • So you receive the contents of a gif file? Well then there is nothing to convert i would think. – greenapps Oct 20 '16 at 11:41
  • What is your exact problem? If you can do it with a local gif from file then why not with a gif in memory? You could first write those bytes to file if you only can handle gifs in files. – greenapps Oct 20 '16 at 11:46
  • I have changed the question and added code samples so that you understand my situation better – BelgishChoko Oct 21 '16 at 10:31

1 Answers1

1

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();
}
BelgishChoko
  • 393
  • 2
  • 12