Glide - download and resize GIF into a file

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-05 05:42:26

问题


I need to download a GIF and save it to external storage so I can send it via MMS.Messages have a limit 300kb and most of the GIFs are too large so I need to resize them.

I am using Glide in rest of my project and Glide has a nifty function which should, in theory, download a resized image. But it doesn't.

In short, here's the code I'm calling inside a background thread:

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

FileOutputStream fileWriter = new FileOutputStream(file);
fileWriter.write(bytes);
fileWriter.flush();
fileWriter.close();

Downloaded files still keep their original size which is above the MMS limit of 300kb whereas they should be 250x250 pixels.


回答1:


you can do compress that file also and then store in database this link will help you. in this first convert .gif to .png then use it. Android - Best way to convert .gif to .png




回答2:


Asked the same question on Glide Github and got the answer there. Apparently Glide will only resize images that are larger than 500 x 500, unless a fitCenter() (or any other) transformation is used.

So, to resize the .gif file, use this code:

byte[] bytes = Glide.with(context)
                        .load(url)
                        .asGif()
                        .toBytes()
                        .transform(new GifDrawableTransformation(new CenterCrop(context), Glide.get(context).getBitmapPool()))
                        .into(250, 250)
                        .get();


来源:https://stackoverflow.com/questions/40129543/glide-download-and-resize-gif-into-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!