[英]Cache images local, from google 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);
歡迎任何幫助。 謝謝!
我擔心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:要使用Picasso進行磁盤緩存,您需要顯式設置OkHttpDownloader。 在這里查看如何在Picasso中使用磁盤緩存?
感謝@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
添加依賴build.gradle
:
implementation 'com.firebaseui:firebase-ui-:3.1.0'
老答案:
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);
我遇到過同樣的問題。 嘗試了所有可能的方法,但無法解決這個問題。 我認為問題在於firebase存儲生成的鏈接。 畢加索通常會緩存它加載的圖像。 我嘗試了其他雲服務,如cloudinary,LibPixel,其鏈接以圖像格式(例如:.jpg)結束,畢加索緩存這些鏈接圖像。 雖然firebase鏈接以令牌號結束。 奇怪的是它失敗了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.