glide icon indicating copy to clipboard operation
glide copied to clipboard

Encode Gif to Base64

Open DaveMurchison83 opened this issue 2 years ago • 1 comments

I'm trying to encode an an animated Gif to base64 so I can upload it from an Android app to my server, where I'll decode it and save it as a file.

I've used this code:

(ImagePath is a TextView that holds the path to the GIF on my device)

I'm using Glide v4.11.0.

                              Glide.with(context)
                                .as(byte[].class)
                                .load(imagePath.getText().toString())
                                .into(new CustomTarget<byte[]>() {
                                    @Override
                                    public void onResourceReady(@NonNull byte[] resource, @Nullable Transition<? super byte[]> transition) {

                                       String attachImage = Base64.encodeToString(resource,
                                                Base64.NO_WRAP);

                                    }

                                    @Override
                                    public void onLoadCleared(@Nullable Drawable placeholder) {

                                    }
                                });

But when it submits to my server and I convert to a file, it doesn't animate. I'm unsure if it's the code above that is only encoding the first frame, or if it is something I am doing on my server when decoding and saving the file that is the issue. So I wanted to check if my above code is correct for converting an animated Gif to base64.

Thanks.

DaveMurchison83 avatar Oct 13 '21 10:10 DaveMurchison83

We have to use Gif Encoder its seems. There is a class called AnimatedGifEncoder and ReEncodingGifResourceEncoder. I want to use them for resing the gif and save it as file but no idea about it

https://github.com/bumptech/glide/blob/a7b254c42888db691b7339b2165abcd462473fcf/third_party/gif_encoder/src/main/java/com/bumptech/glide/gifencoder/AnimatedGifEncoder.java

https://github.com/bumptech/glide/blob/d2bb3e8a6a2296b20ecaeb7759c16ef15aacbb28/integration/gifencoder/src/main/java/com/bumptech/glide/integration/gifencoder/ReEncodingGifResourceEncoder.java

KishorJena avatar Mar 20 '22 09:03 KishorJena