代码之家  ›  专栏  ›  技术社区  ›  Foobar

滑动-将gif保存到文件

  •  2
  • Foobar  · 技术社区  · 7 年前

    SimeNoTo--虽然我的一些例子在Kotlin,Java或Kotlin的答案都是有用的。

    我正在使用 Commit Content API . 要做到这一点,我需要能够创造 .gif 文件夹。

    我正在使用glide从Internet获取图像,并将其显示在 recyclerview .

    对于每个图像滑动下载,我必须创建一个 File 对象中包含该图像的数据。

    我看到有两种方法可以实现这一目标。

    1]创建一个覆盖 onResourceReady 像这样。这个方法的问题是我不知道如何转换 GifDrawable GIF 因为所有的堆栈溢出帖子都讨论了如何转换 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 或者类似的东西。示例Java代码,从帖子中获取 Glide - download and resize GIF into a file ,可能看起来像这样。这个代码的问题是

    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的文件?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Foobar    6 年前

    如果您只需要content comit api的uri,那么update@nh&--t_n的解决方案可能更好。

    我不知道@nhát tn的解决方案是否有效。但是,这是我现在使用的解决方案,它基于他们的解决方案。我的解决方案是在Kotlin,但它可以很容易地转换为Java。

    首先,我将图像加载到 ImageView 使用以下代码:

    //items[position] is a string that represents a URL
    Glide.with(context)
            .load(items[position])
            .into(holder.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()
    

    这个解决方案的一个潜在问题是 天才的 可以在写入文件时进行修改。

    解决方法是克隆 天才的 在将其写入文件之前:

    val newGifDrawable = ((holder.imageView.drawable).constantState.newDrawable().mutate()) as GifDrawable
    
        2
  •  1
  •   Nathan Ton MuraliGanesan    7 年前

    如果您不必将所有gif保存在一个特定的位置和/或命名gif,我建议您只使用为您自动缓存的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();