Local cache images from Google Firebase storage

I am looking for a way to cache images from storage on google firebase platform. At the moment, I can upload images and show them to users, but I can’t cache and access them even without an Internet connection. Access to the database can be done offline. So I thought that there would also be a storage method. I don’t want to upload every image to the repository, so I will need to check every time if the image is still relevant, it can be changed. Here are some links that I can find, but there is no answer to my question. Perhaps someone knows a workaround or a way to achieve it. Thanks!

File Download: https://firebase.google.com/docs/storage/android/download-files

Cache (standalone) database: https://firebase.google.com/docs/database/android/offline-capabilities

UPDATE 1

This is how I β€œcache” files using picasso, I added activity that takes care of the upload:

Picasso.with(getApplicationContext()) .load(uri.toString()) .networkPolicy(NetworkPolicy.OFFLINE) .into(image1); 

Any help is appreciated. Thanks!

+7
android caching firebase firebase-storage
source share
3 answers

I'm afraid the Firebase SDK does not provide image caching on its own. But there are some great libraries that could do this for you. They upload an image, display it in an ImageView, and cache it in a single line of code. Just request Firebase for the image upload URL and submit it to the image caching library.

Something like this if you select Picasso as your caching library:

 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: To use disk caching using Picasso, you need to explicitly configure OkHttpDownloader. Look here. How to use disk caching in Picasso?

+8
source share

Thanks @ATom for the notice. Api has changed and FirebaseUI 3.0 now uses Glide 4.x. Here is an updated example:

To download an image from StorageReference, first register in your 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()); } } 

Then you can load the StorageReference into 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); 

And don't forget to add the dependency to build.gradle :

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

GitHub response source

Old answer:

FirebaseUI 1.0 is now released. Storage example has class FirebaseImageLoader

Images displayed using FirebaseImageLoader are cached along their path in Firebase Storage, so repeated loads will be fast and bandwidth will be saved.

  // 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); 
+3
source share

I had the same problem. I tried all possible ways, but could not fix it. I think the problem is with the links generated by the firebase repository. Picasso usually caches the images it uploads. I tried other cloud services such as cloudinary, LibPixel, the links of which end with the image format (for example: .jpg), and Picasso caches these link images. So far, firebase links end with token numbers. Strange that he is failing!

+1
source share

All Articles