2016-06-08 23 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