从谷歌的firebase存储本地缓存图像 [英] Cache images local, from google firebase storage
问题描述
下载文件:
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屋!