CrackerKSR
CrackerKSR

Reputation: 1877

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

Upvotes: 4

Views: 1083

Answers (1)

Sambhav Khandelwal
Sambhav Khandelwal

Reputation: 3765

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.

Upvotes: 3

Related Questions