![](/img/trans.png)
[英]Glide: How to resize and save the gif as file using Glide v4?
[英]Glide - Save GIF to File
旁注-尽管我的一些示例在Kotlin中,但是用Java或Kotlin回答都是有帮助的。
我正在使用Commit Content API创建GIF键盘。 为此,我需要能够创建.gif
文件。
我正在使用Glide从互联网上获取图像,并将其显示在recyclerview
内的列表中。
对于Glide下载的每个图像,我必须使用该图像的数据创建一个File
对象。
我看到可以实现此目标的两种方法。
1]像这样创建一个重写onResourceReady
的侦听器。 这种方法的问题是我不知道如何将GifDrawable
转换为.gif
文件,因为所有Stack Overflow帖子都谈到了如何将Drawable
转换为文件:
Glide.with(context)
.asGif()
.load(items[position])
.listener(object : RequestListener<GifDrawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<GifDrawable>?, isFirstResource: Boolean): Boolean {
Log.d("VED-APP","Glide Load Failed")
return false
}
override fun onResourceReady(resource: GifDrawable?, model: Any?, target: Target<GifDrawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
//I need code to convert the GifDrawable to a File
return false
}
})
.into(holder.imageView)
2]将图像加载到ByteArray
或类似的东西。 摘自Glide的示例Java代码-将GIF下载并调整大小为文件 ,看起来可能像这样。 这段代码的问题是
A] .into(width,height)
已弃用
B]没有.toBytes
方法
C]我不想指定GIF的尺寸-我只想使用原始尺寸。 (或者至少,我想保留宽高比)
byte[] bytes = Glide.with(context)
.load(items[position])
.asGif()
.toBytes()
.into(250, 250)
.get();
file = new File(fileName);
FileOutputStream fileWriter = new FileOutputStream(file);
fileWriter.write(bytes);
fileWriter.flush();
fileWriter.close();
有人可以帮助我改善这两种方法中的任何一种,还是建议使用Glide将GIF转换为文件的新方法?
如果只需要内容提交API的URI,则更新@NhttTôn的解决方案可能更好。
我不知道@NhttTôn的解决方案是否有效。 但是,这是我现在正在使用的解决方案,它基于他们的解决方案。 我的解决方案在Kotlin中,但是可以轻松地转换为Java。
首先,使用以下代码将图像加载到ImageView
:
//items[position] is a string that represents a URL
Glide.with(context)
.load(items[position])
.into(holder.imageView)
然后,我从ImageView
获取GifDrawable
并将其写入File
对象。
val byteBuffer = (holder.imageView.drawable as GifDrawable).buffer
val gifFile = File(localDirectory, "test.gif")
val output = FileOutputStream(gifFile)
val bytes = ByteArray(byteBuffer.capacity())
(byteBuffer.duplicate().clear() as ByteBuffer).get(bytes)
output.write(bytes, 0 ,bytes.size)
output.close()
该解决方案的潜在问题是,将GifDrawable
写入文件时可以对其进行修改。
解决此问题的方法是在将GifDrawable
写入文件之前像这样克隆GifDrawable
:
val newGifDrawable = ((holder.imageView.drawable).constantState.newDrawable().mutate()) as GifDrawable
如果您不必将所有gif保存在一个特定的位置,也不必将其命名为gif,建议您使用Glide自动为您缓存的gif。
因此,首先,在您的OnBindViewHolder方法中,将gif照常加载到imageview中:
Glide.with(this)
.load(items[position])
.apply(new RequestOptions()
// This is to cache all versions of the gif, whether low-res or original size
.diskCacheStrategy(DiskCacheStrategy.ALL))
.into(holder.imageView);
并使用此方法在您想要的任何地方获取uri:
Glide.with(this)
// Actually it won't download another time if the file has been cached
.download(items[position])
.listener(new RequestListener<File>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
//Use this uri for the Commit Content API
Uri uri = Uri.fromFile(resource);
return false;
}
})
.submit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.