旁注-虽然我的一些例子是用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
或类似的文件中。取自post Glide - download and resize GIF into a file的示例Java代码可能如下所示。这段代码的问题是
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();
有人能帮我改进这两种方法中的任何一种吗,或者建议一种新的方法将GIF转换为Glide文件?
发布于 2018-06-04 02:50:03
如果您只需要content comitậ的URI,那么Update @Nh comit t Tán的解决方案可能会更好。
我不知道@Nhật Tó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
写入文件之前像这样克隆它:
val newGifDrawable = ((holder.imageView.drawable).constantState.newDrawable().mutate()) as GifDrawable
发布于 2018-06-03 16:32:34
如果你不需要在一个特定的位置保存所有的gif或者命名gif,我建议你使用Glide自动缓存的gif。
所以首先,在你的OnBindViewHolder方法中,像往常一样把gif加载到你的图像视图中:
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();
https://stackoverflow.com/questions/50663369
复制相似问题