2016-06-08 71 views
7

我正在寻找一种方法来缓存来自google firebase平台上存储的图片。目前,我可以下载图像,并将这些图像显示给用户,但我无法缓存此内容,并且无法访问,即使没有网络连接。数据库可以脱机访问。所以我想,也会有一种存储方式。我不想将每张图像下载到存储器,因为那么我需要每次检查,如果图像仍然是最新的,它可能会改变。这里有几个链接,我可以找到,但没有回答我的问题。也许有人知道一种解决方法,或者一种方式如何实现它。谢谢!本地缓存图片,来自google firebase storage

下载文件: 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); 

欢迎任何帮助。谢谢!

+0

你可以自己缓存图片吗? LRU和其他类型 – Shubhank

回答

8

恐怕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 
    } 
}); 

UPD:要使用磁盘缓存与毕加索,你需要明确设置OkHttpDownloader。看这里How do I use disk caching in Picasso?

+2

我已经尝试过毕加索,完全按照您提供的方式。事情是,如果我重新启动应用程序,没有互联网,图像不显示...有什么我不得不说,存储图像?或者,也许我需要许可存储? – Wladislaw

+0

对,有一个问题:要在Picasso上使用磁盘缓存,你需要明确地设置OkHttpDownloader。看这里http://stackoverflow.com/questions/23978828/how-do-i-use-disk-caching-in-picasso –

+0

好吧,我实现了这一个。现在我可以看到蓝色的符号,这意味着它在磁盘上。但是,每当我断开我与无线网络,并成为离线,这就是我在Logcat ...'W/ExponenentialBackoff:网络不可用,睡觉.' – Wladislaw

1

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

3

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

图片显示,如此反复负荷将快速且节约 带宽。

// 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); 
+0

此答案仅适用于Glide 3.X和旧版本的FirebaseUI。在GLide 4.X中,没有使用()的方法。 – ATom

相关问题