Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 329
CrackerKSR Avatar asked Oct 31 '25 06:10

CrackerKSR


1 Answers

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.

like image 178
Sambhav Khandelwal Avatar answered Nov 01 '25 19:11

Sambhav Khandelwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!