Wla*_*law 8 android caching firebase firebase-storage
我正在寻找一种方法,从谷歌firebase平台上的存储缓存图像.现在,我可以下载图像,并向用户显示这些图像,但即使没有互联网连接,我也无法缓存此图像并进行访问.可以脱机访问数据库.所以我想,还有一种存储方式.我不想将每个图像下载到存储器,因此我需要每次检查,如果图像仍然是最新的,它可能会被更改.这里有一些链接,我能找到的,但我的问题没有答案.也许有人知道一种解决方法,或者一种如何实现它的方法.谢谢!
下载文件:https: //firebase.google.com/docs/storage/android/download-files
缓存(离线)数据库:https: //firebase.google.com/docs/database/android/offline-capabilities
更新1
这是我用毕加索"缓存"文件的方式,我添加了关注下载的活动:
Picasso.with(getApplicationContext())
.load(uri.toString())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(image1);
Run Code Online (Sandbox Code Playgroud)
欢迎任何帮助.谢谢!
我担心Firebase SDK本身不提供图像缓存.但是有几个很棒的库可以帮到你.他们下载图像,在ImageView中显示它并将其缓存在一行代码中.只需向Firebase请求图片下载网址并将其提供给图片缓存库.
如果您选择Picasso作为缓存库,那么这样的事情:
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
}
});
Run Code Online (Sandbox Code Playgroud)
UPD:要使用Picasso进行磁盘缓存,您需要显式设置OkHttpDownloader.在这里查看如何在Picasso中使用磁盘缓存?
感谢@ATom的通知.Api已更改,FirebaseUI 3.0现在使用Glide 4.x这是更新的示例:
要从StorageReference加载图像,请首先在AppGlideModule中注册:
Run Code Online (Sandbox Code Playgroud)@GlideModule public class MyAppGlideModule extends AppGlideModule { @Override public void registerComponents(Context context, Glide glide, Registry registry) { // Register FirebaseImageLoader to handle StorageReference registry.append(StorageReference.class, InputStream.class, new FirebaseImageLoader.Factory()); } }然后,您可以将StorageReference加载到ImageView中:
Run Code Online (Sandbox Code Playgroud)// Reference to an image file in Cloud Storage StorageReference storageReference = ...; // ImageView in your Activity ImageView imageView = ...; // Download directly from StorageReference using Glide // (See MyAppGlideModule for Loader registration) GlideApp.with(this /* context */) .load(storageReference) .into(imageView);
并且不要忘记在您的内容中添加依赖项build.gradle:
implementation 'com.firebaseui:firebase-ui-:3.1.0'
Run Code Online (Sandbox Code Playgroud)
老答案:
FirebaseUI 1.0现已发布.存储示例具有类FirebaseImageLoader
使用FirebaseImageLoader显示的图像由其在Firebase存储中的路径缓存,因此重复加载将很快并节省带宽.
// Reference to an image file in Firebase Storage
StorageReference storageReference = ...;
// ImageView in your Activity
ImageView imageView = ...;
// Load the image using Glide
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(storageReference)
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12233 次 |
| 最近记录: |