从谷歌firebase存储缓存本地图像

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)

欢迎任何帮助.谢谢!

Ale*_*kov 9

我担心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中使用磁盘缓存?

  • 就是这样,但是要从缓存中获取文件,我需要指定它的原始URL,我只能从firebase存储中获取.我错过了什么? (7认同)
  • Firebase存储与数据库分开,这个`setPersistenceEnabled(true)`不起作用.我们正在研究用于启用离线用例(并提高性能)的内置缓存,但我们还没有具体的计划,因此我们建议使用像Picaso,Glide和SDWebImage(iOS)这样的图像加载库案件. (3认同)
  • 我已经尝试了毕加索,就像你提供的方式一样.问题是,如果我重新启动应用程序,没有互联网,图像不会显示...有什么我必须说,存储图像?或者我可能需要存储许可? (2认同)

Dim*_*ira 6

感谢@ATom的通知.Api已更改,FirebaseUI 3.0现在使用Glide 4.x这是更新的示例:

要从StorageReference加载图像,请首先在AppGlideModule中注册:

@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());
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以将StorageReference加载到ImageView中:

// 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);
Run Code Online (Sandbox Code Playgroud)

并且不要忘记在您的内容中添加依赖项build.gradle:

implementation 'com.firebaseui:firebase-ui-:3.1.0'
Run Code Online (Sandbox Code Playgroud)

在GitHub上回答消息来源

老答案:

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)