使用Kotlin在Fresco中获取GIF文件

时间:2018-06-03 02:14:17

标签: android kotlin fresco

我有一个recyclerview,它使用Fresco(一个Android库)显示GIF格式的图像列表。

当用户点击recyclerview中的图片时,我需要能够获取.gif文件并将其存储为somename.gif

我试过这段代码:

val contentRequest = ImageRequest.fromUri(items[position])
val cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(contentRequest, null)
val imageResource = ImagePipelineFactory.getInstance().mainFileCache.getResource(cacheKey)
val file = (imageResource as FileBinaryResource).file

Log.d("VED-APP", file.name)

但结果最终是:Lw0g1Jq7J0jUxSCCEZe3UwRa6-0.cnt,这与" somename.gif"不同。

所以,我尝试了这个代码:

val contentRequest = ImageRequest.fromUri(items[position])
val imagePipeline = Fresco.getImagePipeline()
val dataSource : DataSource<CloseableReference<CloseableImage>> =
                    imagePipeline.fetchDecodedImage(contentRequest, null)

但问题是,我需要将对象dataSource写入文件。

我发现此代码示例可以帮助我将dataSource写入文件,但我不知道如何将其转换为Kotlin。有人可以帮我转换吗?

或者,如果有人知道从Fresco获取文件的更好方法,他们可以建议另一种方法吗?

DataSource<CloseableReference<T>> dataSource = ...;

DataSubscriber<CloseableReference<T>> dataSubscriber =
    new BaseDataSubscriber<CloseableReference<T>>() {
      @Override
      protected void onNewResultImpl(
          DataSource<CloseableReference<T>> dataSource) {
        if (!dataSource.isFinished()) {
          // if we are not interested in the intermediate images,
          // we can just return here.
          return;
        }
        CloseableReference<T> ref = dataSource.getResult();
        if (ref != null) {
          try {
            // do something with the result
            T result = ref.get();
            ...
          } finally {
            CloseableReference.closeSafely(ref);
          }
        }
      }

      @Override
      protected void onFailureImpl(DataSource<CloseableReference<T>> dataSource) {
        Throwable t = dataSource.getFailureCause();
        // handle failure
      }
    };

dataSource.subscribe(dataSubscriber, executor);

重要更新一次 -

我想出了如何将上述大部分代码转换为Kotlin。唯一的问题是,我不知道如何使用适当的executor订阅dataSubscriber,请参阅下面的代码以澄清。

根据documentation,我应该使用后台线程执行程序,但我不确定如何在Kotlin中执行此操作。

        val dataSubscriber : DataSubscriber<CloseableReference<CloseableImage>> =
                object : BaseDataSubscriber<CloseableReference<CloseableImage>>() {
                    override fun onNewResultImpl(dataSource: DataSource<CloseableReference<CloseableImage>>?) {

                        if(!dataSource!!.isFinished) {
                            return
                        }

                        val ref : CloseableReference<CloseableImage>? = dataSource.result

                        if(ref != null) {
                            try {
                                val result = ref.get()
                            } finally {
                                CloseableReference.closeSafely(ref)
                            }
                        }
                    }

                    override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage>>?) {
                        Log.d("VED-APP","Fresco Failed to Fetch?")
                    }
                }

        val contentRequest = ImageRequest.fromUri(items[position])
        val imagePipeline = Fresco.getImagePipeline()

        val dataSource : DataSource<CloseableReference<CloseableImage>> =
                imagePipeline.fetchDecodedImage(contentRequest, null)

        dataSource.subscribe(dataSubscriber,/*I don't know what to put here */)

0 个答案:

没有答案