Glide:如何使用 Glide v4 调整大小并将 gif 另存为文件?

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

我想调整 gif 文件的大小并保存。我尝试使用一些建议的方法,但这些方法会出错,后来我才知道 Glide v4

中不推荐使用某些方法
           byte[] bytes = Glide.with(context)
                         .asGif()                   
                         .load(url)
                         .toBytes()
                         .into(250, 250)
                         .submit()
                         .get();

在上面的代码中,将数组转换为文件会得到 4.x MB 大小的空白 gif 文件

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

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

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

但是上面的代码保持了宽度和高度不变

详情:

Glide version : 4.13.0

请分享正确的代码或提出调整 gif 大小的建议(保存为文件而不是显示)。

Glide不仅可以加载文件,还可以下载文件。这就是你想要的。您只需使用此代码即可下载。

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;
                }
            });

storeImage方法:

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;
}

编辑


我实际上已经找到了 1 个图书馆。在那个图书馆里,我发现 this class 很有趣。在这里,在构造函数中我们可以传递宽度和高度,然后我们可以保存它。