如何检索加载到SimpleDraweeView的图像/ Gif文件-Android

时间:2019-07-17 11:31:50

标签: android file fresco

因此,基本上,我想访问已加载到SimpleDraweeView中的Image或Gif的文件/ ByteArray。当Fresco从URL下载图像时,由于这些图像没有变化,因此我想将该图像保存在数据库中以供将来访问(以帮助减少数据使用量)。这样,下次我想将图像加载到SimpleDraweeView时,我将加载本地媒体,而无需从url重新下载。

在向Fresco.newDraweeControllerBuilder添加onFinalImageSet的位置添加侦听器之后,我尝试了几种方法:

1-尝试从Fresco用来获取其ByteArray的缓存中访问图像的File,但是,我总是从hasKey() = False得到一个ImagePipelineFactory:< / p>

val imageRequest = ImageRequest.fromUri(mediaUrl)
val cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(imageRequest, null)
if(ImagePipelineFactory.getInstance().mainFileCache.hasKey(cacheKey)) {
    val resource = ImagePipelineFactory.getInstance().mainFileCache.getResource(cacheKey) 
    val file = (resource as FileBinaryResource).file
}else if (ImagePipelineFactory.getInstance().smallImageFileCache.hasKey(cacheKey)){
    val resource = ImagePipelineFactory.getInstance().mainFileCache.getResource(cacheKey) 
    val file = (resource as FileBinaryResource).file
}

2-尝试从imageInfo(作为CloseableStaticBitmap)或animatable(作为CloseableAnimateImage)中获取位图,但是如果我想将动画设置为CloseableAnimateImage出现编译错误“未解决的参考:CloseableAnimateImage”。然后的想法是从位图中获取ByteArray并保存它。不过,我真的很想得到(1)的方法。

关于如何获取缓存文件的任何想法?预先谢谢你!

3 个答案:

答案 0 :(得分:0)

在gradle中添加依赖项

implementation 'com.squareup.picasso:picasso:2.71828'

然后在显示图像的地方

Picasso.get().load("URL").into(target);

向毕加索添加目标

Target target = new Target() {
      @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            imageView.setBitmap(bitmap)
            //You can do whatever you want to do with bitmap

      }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
}

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
}};

答案 1 :(得分:0)

使用滑行:

GlideApp.with(context)
            .load(url)
            .into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    // save image here
                }
            });

使用毕加索:

Target target = new Target() {
      @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            // save image here
      }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
    };

    Picasso.with(this).load(url).into(target);

答案 2 :(得分:0)

最后,直到有人可以用SimpleDraweeView回答如何做,我才决定同时申请Glide。毕加索不是一个选择,因为它不支持gif,这是我的问题中的要求之一。此外,我想继续在ImageView中显示加载的图像,因此我不想将into更改为自定义目标。

因此,使用Glide的解决方案如下:

首先,我们尝试通过tryToGetFromMyDatabase在数据库中查找下载的媒体。如果找到它,我们将继续简单地向ByteArray加载Glide。万一找不到存储在数据库中的媒体,tryToGetFromMyDatabase将返回null,因此我们将继续使用Glide加载URL,并在Glide完成下载媒体时进行监听。完成后,我们从Glide的缓存中检索文件,并将ByteArray保存在我的数据库中。

该方法的代码如下:

val url: String = "http://myUrl"
val mediaByteArray: ByteArray? = tryToGetMediaFromMyDatabase(url)
val myImageView: ImageView = findViewById(R.id.image_view)

if(mediaByteArray == null){
    //We don't have the media saved in our database se we download from url and on response save it in database
    var builder = if(isGif){
            Glide.with(context)
            .asGif()
            .load(mediaUrl)
            .addListener(object: RequestListener<GifDrawable> {
                override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<GifDrawable>?, isFirstResource: Boolean): Boolean {
                    return false
                }

                override fun onResourceReady(resource: GifDrawable?, model: Any?, target: Target<GifDrawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                    onMediaLoaded(mediaUrl)
                    return false
                }
            })

        }else{
            Glide.with(context)
            .asBitmap()
            .load(mediaUrl)
            .addListener(object: RequestListener<Bitmap>{
                override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                    return false
                }

                override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                    onMediaLoaded(mediaUrl)
                    return false
                }

            })
        }

builder.into(myImageView)

}else{
    //We have the media saved in database so we just load it without downloading it from the url
    var builder = if(isGif){
            Glide.with(context).asGif()
        }else{
            Glide.with(context).asBitmap()
        }
    builder.load(mediaByteArray).into(myImageView)
}

我的onMediaLoaded(url: String)执行以下操作以从Glide缓存中获取文件并将其保存到我的数据库中:

Thread(Runnable {
    val imageBytes = Glide.with(context)
            .asFile()
            .load(url)
            .submit()
            .get()
            .readBytes()
    saveIntoDatabase(url, imageBytes)
})