【问题标题】:Cache images local, from google firebase storage本地缓存图像,来自 google firebase 存储
【发布时间】:2016-10-08 13:20:17
【问题描述】:

我正在寻找一种方法,从 google 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);

欢迎任何帮助。谢谢!

【问题讨论】:

  • 你可以自己缓存图片吗? LRU等类型

标签: android caching firebase firebase-storage


【解决方案1】:

恐怕 Firebase SDK 本身不提供图像缓存。但是有几个很棒的库可以为你做这件事。他们下载图像,将其显示在 ImageView 中并将其缓存在一行代码中。只需向 Firebase 请求图像下载 url 并将其提供给图像缓存库。

类似这样,如果您选择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
    }
});

UPD:要在 Picasso 中使用磁盘缓存,您需要显式设置 OkHttpDownloader。看这里How do I use disk caching in Picasso?

【讨论】:

  • 我已经尝试过毕加索,完全按照您提供的方式。问题是,如果我在没有互联网的情况下重新启动应用程序,图像就不会显示出来……我有什么要说的来存储图像吗?或者也许我需要存储权限?
  • 对,有一个问题:要在 Picasso 中使用磁盘缓存,您需要显式设置 OkHttpDownloader。看这里stackoverflow.com/questions/23978828/…
  • Firebase 存储与数据库分开,这个setPersistenceEnabled(true) 不起作用。我们正在研究用于启用离线用例(并提高性能)的内置缓存,但我们还没有具体计划,因此我们建议使用 Picaso、Glide 和 SDWebImage (iOS) 等图像加载库来实现此用途案例。
  • 是这样,但是要从缓存中获取文件,我需要指定它的原始 url,我只能从 firebase 存储中获取。我错过了什么吗?
  • 当您存储图像时,您会得到一个带有 downloadURL 的快照 - 将此 URL 存储在数据库中而不是图像名称,因此您不必执行 storageRef.child("users/me/ profile.png").getDownloadUrl()
【解决方案2】:

感谢@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());
    }
}

然后您可以将 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);

别忘了在 build.gradle 中添加依赖:

implementation 'com.firebaseui:firebase-ui-:3.1.0'

Answer source on 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);

【讨论】:

  • 这个答案仅对 Glide 3.X 和旧版本的 FirebaseUI 有效。在 GLide 4.X 中还没有 using() 方法。
  • 如果拍摄新照片,此方法是否可以离线使用?
  • @VivekRajendran 不,它没有,您需要自己处理这种情况
  • 如何限制缓存大小?我使用了这个实现,它运行良好,但是当缓存大小开始增长时,应用程序开始需要 30 秒来加载数据。
【解决方案3】:

我有同样的问题。尝试了所有可能的方法,但无法解决。我认为问题在于firebase存储生成的链接。 Picasso 通常会缓存它加载的图像。我尝试过其他云服务,如 cloudinary、LibPixel,其链接以图像格式(例如:.jpg)结尾,毕加索缓存这些链接图像。而firebase链接以令牌编号结尾。奇怪的是它失败了!

【讨论】:

    猜你喜欢
    • 2019-04-29
    • 2019-05-03
    • 2018-03-02
    • 1970-01-01
    • 2019-06-11
    • 2021-05-21
    • 2020-09-13
    • 2016-09-28
    • 1970-01-01
    相关资源
    最近更新 更多