14

I want to show a photo series with no gaps in-between the photos, where photos change in a regular interval. I realized Picasso initializes the ImageView before it starts downloading, and it always does that, no matter if I fetch() or not before calling into().

I fetch() to keep the gap between images small and also use .placeholder(R.color.black), but the gap is still visible, even when the image is loaded from memory.

My code looks like this

Picasso.with(getContext()).load(url).fetch();

then with a delay [which is currently fix and which I want to adjust dependent on network speed]

Picasso.with(getContext()).load(url).into(screenSurface);

I noticed that fetch() does not support any callback parameters and returns void, so it seems it's not possible for me to know when the cache is warmed.

Two questions:

  1. Can I get noticed when an image is cached?
  2. Is there maybe a different way to get rid of the breaks between the images and make them appear regularly.

[I know I could manually code this somehow, but if Picasso supports it, I'd like to use it.]

Oliver Hausler
  • 4,900
  • 4
  • 35
  • 70

5 Answers5

17

Based on the source, it looks like fetch does nothing upon completion, including notifying any potential listeners. Unfortunately, FetchAction isn't a public class, so you can't override this functionality either.

You can workaround this problem by using a custom Target subclass, like this:

Picasso.with(getContext()).load(url).into(new Target() {
    @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        // cache is now warmed up
    }
    @Override public void onBitmapFailed(Drawable errorDrawable) { }
    @Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
});
bmat
  • 2,084
  • 18
  • 24
  • This is very nice, but I realized I cannot use functions like .fit() or .centerCrop() as I used them before, which is not a problem as I can set the appropriate attributes in the ImageView. But what I would like to know is if using Target() has any implications on caching images. Are the images cached as downloaded so I could technically use them in differently sized previews? Or will I need to .resize() manually or anything the like? – Oliver Hausler Oct 09 '14 at 17:28
  • 2
    You can call `load(url).fit().into(screenSurface)` after using the workaround in my solution, and as long as `url` is the same, I believe it will be loaded from the cache. – bmat Oct 09 '14 at 19:55
5

I know this is an older question but the fetch() command allows for callbacks such as fetch(Callback callback). This has an onSuccess() and onError() callback for the requested URI load.

See here for javadoc details

Duncan Hoggan
  • 5,082
  • 3
  • 23
  • 29
0

The .into() method provides a second argument which is a callback to success and failure. You can use this to keep track of when an image has arrived.

Javadoc: http://square.github.io/picasso/javadoc/com/squareup/picasso/RequestCreator.html#into-android.widget.ImageView-com.squareup.picasso.Callback-

Have a look at this: How to implement my own disk cache with picasso library - Android? Jake Wharton himself has an answer there.

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Thanks, I am aware of the Callback in into() and I am using it, but to get a smooth image sequence, I need the information if/when the image is loaded BEFORE into() is called. I will look into implementing my own disk cache, which may solve the problem. – Oliver Hausler Oct 09 '14 at 16:39
0

As of version 2.7 of Picasso, it does support callback with the fetch() method.

Picasso.get()
                    .load(url)
                    .fetch(new Callback() {
                        @Override
                        public void onSuccess() {
                            //Success
                        }
                        @Override
                        public void onError(Exception e) {
                            //Error
                        }});
Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
0

I tried noPlaceholder method to resolve the problem. I have tried many methods, but eventually found that the question is to make the previous picture wait for next picture.

Prashant_M
  • 2,868
  • 1
  • 31
  • 24