Glide: How to resize and save the gif as file using Glide v4?
Asked Answered
T

1

4

I want to resize the gif file and save it. I tried to use some suggested methods but those give error and later I came to know that some of methods are deprecated in Glide v4

           byte[] bytes = Glide.with(context)
                         .asGif()                   
                         .load(url)
                         .toBytes()
                         .into(250, 250)
                         .submit()
                         .get();

In above code converting the arrays to file gives blank gif file with 4.x MB size

            File file = Glide.with(reactContext)
                        .asFile()
                        .load(url)
                        .override(512, 512)
                        .fitCenter()
                        .into(512,512)
                        .get();

And

            File file = Glide.with(reactContext)
                        .asFile()
                        .load(url)
                        .apply(new RequestOptions().override(512, 512))
                        // .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .submit(512,512)
                        .get();

And

            File file = Glide.with(reactContext)
                        .asFile()
                        .load(url)
                        // .override(512, 512)
                        .fitCenter()
                        .submit(512,512)
                        .get();

But the above code keeps the width and height as it is

Details:

Glide version : 4.13.0

Please share the proper code or suggest something to resize the gif (to save as file rather displaying).

Tanto answered 12/3, 2022 at 19:1 Comment(6)
what glide version are you using?Aculeate
have you added these 2 libraries? -> If not, add theme implementation 'com.github.bumptech.glide:glide:4.13.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'Aculeate
asGif works now but can't save as file. the file size is 4mb but the file is blankTanto
I saw there is asFile type in glide's repo so I tried with asFile(). But I could not resize the gif using override(size) and submit(size).Tanto
If you know Glide well , Can you please tell me how to resize the gif file for saving not displaying on imageviewTanto
As my question title implies, I want to resize and save the gif :)Tanto
A
3

Glide can not only load files, but also download them. And that is what you want. You can just use this code and it will be downloaded.

Glide.with(MainActivity.this).asFile()
            .load(url)
            .apply(new RequestOptions()
                    .format(DecodeFormat.PREFER_ARGB_8888)
                    .override(Target.SIZE_ORIGINAL)) // you can also give your size here
            .into(new Target<File>() {
                @Override
                public void onStart() {

                }

                @Override
                public void onStop() {

                }

                @Override
                public void onDestroy() {

                }

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

                }

                @Override
                public void onLoadFailed(@Nullable Drawable errorDrawable) {

                }

                @Override
                public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
                    storeImage(resource);
                }

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

                }

                @Override
                public void getSize(@NonNull SizeReadyCallback cb) {

                }

                @Override
                public void removeCallback(@NonNull SizeReadyCallback cb) {

                }

                @Override
                public void setRequest(@Nullable Request request) {

                }

                @Nullable
                @Override
                public Request getRequest() {
                    return null;
                }
            });

The storeImage method:

private void storeImage(File image) {
    File pictureFile = getOutputMediaFile();
    if (pictureFile == null) {
        return;
    }
    try {
        FileOutputStream output = new FileOutputStream(pictureFile);
        FileInputStream input = new FileInputStream(image);

        FileChannel inputChannel = input.getChannel();
        FileChannel outputChannel = output.getChannel();

        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
        output.close();
        input.close();
        Toast.makeText(MainActivity.this, "Image Downloaded", Toast.LENGTH_SHORT).show();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private File getOutputMediaFile() {
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Diwali Images"); // change the folder name according to your needs.
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs())
            return null;
    }

    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_SHUBH_DIWALI_"+Calendar.getInstance().getTimeInMillis() +".gif"); // change the name of the file according to your wish
    return mediaFile;
}

Edit


I have actually found out 1 library for that. In that library, I found this class interesting. Here, in the constructor we can pass the width and height and then we can save it.

Aculeate answered 13/3, 2022 at 9:58 Comment(14)
Thank you so much for the effeort. Code is creating gif file but size is still as original . II tried override(512,512) and override(512) as well.Tanto
I read some discussion on issues on glide repo. There is Gifencoder class. It seems we need that to manipulate gif. well I am not sure about that.Tanto
Here is a snippet but that class ( GifResourceEncoder ) is not there in glide v4 github.com/bumptech/glide/issues/1570#issuecomment-258636999 although I found similar class ( ReEncodingGifResourceEncoder ) here github.com/bumptech/glide/blob/…Tanto
Hi @CrackerKSR. I might be too late to answer, but I have found out 1 thing and so have edited the answerAculeate
Thank you @sambhav-khandelwal. It works like charm. bye bye to Glide and other library :D.Tanto
Decode GIf > iterate each fram > resizeed with respect to aspect ratio using android.graphics.* classes > and then decode them into gif again. I am using this for React Native as there is not module for react native to resize animated images. You saved my grass man. Thanks a lot.Tanto
Hi @sambhav-khandelwal, do you have solution for animated webp encoding ? I found one but it does not support alpha channel so raising excepion github.com/b4rtaz/android-webp-encoder/issues/2Tanto
Sorry to say but I have never felt a need for animated webp and so have never used. Sorry for that. You can post a question asking that and surely someone like me will help you out 🙂Aculeate
No problem. :). I have been googling and already asked question about it then thought I should try to ask you also. Btw It is really shocked to know that in 2022 android reached version 12 but no easy way out there for manipulating 'animated' Webp. although android has built in encoder for static webp like Bitmap.compress.Webp.Tanto
Is there anything wrong in my code for it being unaccepted? I will surely try fixing itAculeate
No that happened unintentionally. thanks to informTanto
Hi Sambhav Can you share your social media profile?Tanto
twitter or telegramTanto
Ok. No problem..Tanto

© 2022 - 2024 — McMap. All rights reserved.