从谷歌的firebase存储本地缓存图像 [英] Cache images local, from google firebase storage

查看:317
本文介绍了从谷歌的firebase存储本地缓存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来缓存谷歌Firebase平台上存储的图片。现在,我可以下载图像,并将这些图像显示给用户,但是我无法缓存,访问,即使没有互联网连接。数据库可以脱机访问。所以我想,也会有一种存储的方式。我不想下载每一个图像到存储器,因此我需要每次检查,如果图像仍然是最新的,它可能会改变。这里有几个链接,我可以找到,但没有回答我的问题。也许有人知道一个解决方法,或者一个方法如何实现它。感谢!



下载文件:
https://firebase.google.com/docs/storage/android/download-files



缓存(离线)数据库:
https://firebase.google.com/docs/database/ android / offline-capabilities

更新1

如何使用picasso缓存文件,我添加了活动,关注下载:

  Picasso.with(getApplicationContext()) 
.load(uri.toString())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(image1);

欢迎任何帮助。感谢!

解决方案

恐怕Firebase SDK本身并不提供图片缓存。但有几个伟大的图书馆可以为你做。他们下载图像,在ImageView中显示,并将其缓存在一行代码中。只需向Firebase申请一个图片下载网址,然后将其提供给图片缓存库即可。



就像这样,如果您选择毕加索作为一个缓存库:

 <$ c ()(){$ c> storageRef.child(users / me / profile.png)。getDownloadUrl()。addOnSuccessListener(new OnSuccessListener< Uri>(){
@Override $ b $ public void onSuccess(Uri uri){
//获得'users / me / profile.png'的下载URL
//传递给毕加索下载,显示ImageView并缓存
Picasso.with(context).load (uri.toString())。into(imageView);
}
})。addOnFailureListener(new OnFailureListener(){
@Override $ b $ public void onFailure(@NonNull异常异常){
//处理任何错误
}
});

UPD:要在Picasso中使用磁盘缓存,您需要明确地设置OkHttpDownloader。看这里如何在毕加索使用磁盘缓存?


I am searching for a way, to cache images from the storage on google firebase platform. For now, I can download images, and show these to users, but I am not able to cache this, and access, even without internet connection. The database can be accessed offline. So I thought, there would be a way for storage too. I don't want to download every single image to storage, cause then I would need to check everytime, if the image is still up to date, it may be changed. Here are few links, what I could find, but no answer for my question. Maybe someone know a workaround, or a way how to accomplish it. Thanks!

Download files: https://firebase.google.com/docs/storage/android/download-files

Cache (offline) database: https://firebase.google.com/docs/database/android/offline-capabilities

UPDATE 1

Here is how I "cache" files with picasso, I added activity, that cares the download:

Picasso.with(getApplicationContext())
                            .load(uri.toString())
                            .networkPolicy(NetworkPolicy.OFFLINE)
                            .into(image1);

Any help is welcome. Thanks!

解决方案

I'm afraid the Firebase SDK doesn't provide image caching by itself. But there are several great libraries that could do it for you. They download image, show it in an ImageView and cache it in a single line of code. Just request the Firebase for an image download url and feed it to the image caching library.

Something like this, if you choose Picasso as a caching library:

storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // Got the download URL for 'users/me/profile.png'
        // Pass it to Picasso to download, show in ImageView and caching
        Picasso.with(context).load(uri.toString()).into(imageView);
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

UPD: To use disk caching with Picasso you need to explicitly setup OkHttpDownloader. Look here How do I use disk caching in Picasso?

这篇关于从谷歌的firebase存储本地缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆