2

I am using Glide to fetch and display photos from Firebase Storage. Things work very well when the device is in online.

When the user updates profile picture (firebase storage reference changes), Glide fails to fetch photo from Firebase storage's offline cache.

I don't want to cache photo using Glide. I just want to retrieve the cached photo from Firebase storage's offline cache. Is there any possible way or workaround to solve this problem.

Here is my code.

Glide Module

@GlideModule
class MyGlideModule : AppGlideModule() {

    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry.append(StorageReference::class.java, InputStream::class.java,
            FirebaseImageLoader.Factory())
    }
}

Code to fetch profile picture from firebase storage

GlideApp.with(profileIcon.context)
                .load(customerPictureReference)
                .centerCrop()
                .placeholder(R.drawable.ic_profile_placeholder)
                .into(profileIcon)
A.Bidusha
  • 43
  • 6
VivekRajendran
  • 676
  • 1
  • 6
  • 21

1 Answers1

2

The issue is that Firebase is not caching images (see here for more details).

You should be able to configure Glide to cache the images locally, with something like (see here more):

@GlideModule
public class GiphyGlideModule extends AppGlideModule {
  @Override
  public void applyOptions(Context context, GlideBuilder builder) {
    builder.setMemoryCache(new LruResourceCache(10 * 1024 * 1024));
  }

  @Override
  public void registerComponents(Context context, Glide glide, Registry registry) {
    registry.append(Api.GifResult.class, InputStream.class, new GiphyModelLoader.Factory());
  }
}

Then you only need to add: .diskCacheStrategy(DiskCacheStrategy.ALL) to your call. Something like:

GlideApp.with(profileIcon.context)
                .load(customerPictureReference)
                .diskCacheStrategy(DiskCacheStrategy.ALL) 
                .centerCrop()
                .placeholder(R.drawable.ic_profile_placeholder)
                .into(profileIcon) 

Another option that you can use is Picasso that will also cache the images.

To retrieve the image first from the local cache, and if it fails to try from online, you can use something like:

Picasso.with(getActivity())
.load(imageUrl)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView, new Callback() {
    @Override
    public void onSuccess() {
      //..image loaded from cache
    }

    @Override
    public void onError() {
        //Try again online if cache failed
        Picasso.with(getActivity())
                .load(posts.get(position).getImageUrl())
                .error(R.drawable.header)
                .into(imageView, new Callback() {
            @Override
            public void onSuccess() {
              //... image loaded from online
            }

            @Override
            public void onError() {
                Log.v("Picasso","Could not fetch image");
            }
        });
    }
});
dan
  • 13,132
  • 3
  • 38
  • 49
  • 1
    You misunderstood the question. My question is how to retrieve photo from firebase storage cache when the device is in offline, not to cache photo by Glide. I have updated question with more details. – VivekRajendran Apr 19 '18 at 06:07
  • @VivekRajendran Maybe you are not clear, but if you will configure the cache, them while online the photos will be retrieved and cached, and using glide or picasso they will be persisted and available while offline too. – dan Apr 19 '18 at 06:17
  • firebase storage itself caches photos for offline purposes. So that I dont want to cache it again with Glide. – VivekRajendran Apr 19 '18 at 07:01
  • @VivekRajendran No, AFAIK, firebase is not providing a way to cache images (see here too: https://stackoverflow.com/questions/37699688/cache-images-local-from-google-firebase-storage/40544554). This is confirmed by the fact that if the images were cached by firebase, they should be retrieved in the same way while offline too, but they are not (you confirmed too with your question). – dan Apr 19 '18 at 07:14
  • If so, I wonder how to upload photo to cloud without having copy of photo? – VivekRajendran Apr 19 '18 at 07:21
  • @VivekRajendran Not sure I understand what you are asking, but if you are wondering how they are able to upload the image without a copy. They don't, they have the image, but are not maintaining it in a cache, once it's done it is discarded. If you are asking something else, please create another question. – dan Apr 19 '18 at 07:24