问题描述
我正在寻找一种方法,从 google firebase 平台上的存储中缓存图像.目前,我可以下载图像,并将这些图像显示给用户,但即使没有互联网连接,我也无法缓存和访问.可以离线访问数据库.所以我想,也会有一种存储方式.我不想将每个图像都下载到存储中,因为我每次都需要检查,如果图像仍然是最新的,它可能会被更改.这里有几个链接,我能找到的,但没有回答我的问题.也许有人知道一种解决方法,或者一种如何完成它的方法.谢谢!
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!
下载文件:https://firebase.google.com/docs/storage/android/download-文件
缓存(离线)数据库:https://firebase.google.com/docs/database/android/offline-能力
更新 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);
欢迎任何帮助.谢谢!
推荐答案
恐怕 Firebase SDK 本身不提供图像缓存.但是有几个很棒的库可以为您完成.他们下载图像,将其显示在 ImageView 中并将其缓存在一行代码中.只需向 Firebase 请求图像下载 url 并将其提供给图像缓存库.
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.
类似这样,如果您选择 Picasso 作为缓存库:
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:要在 Picasso 中使用磁盘缓存,您需要明确设置 OkHttpDownloader.看这里 如何在 Picasso 中使用磁盘缓存?
UPD: To use disk caching with Picasso you need to explicitly setup OkHttpDownloader. Look here How do I use disk caching in Picasso?
这篇关于本地缓存图像,来自 google firebase 存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!