10

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

欢迎任何帮助。谢谢!

4

3 回答 3

9

恐怕 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。看这里如何在 Picasso 中使用磁盘缓存?

于 2016-06-08T11:59:03.390 回答
7

感谢@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'

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);
于 2016-11-11T09:10:56.693 回答
2

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

于 2016-11-09T14:42:38.050 回答