Asthme
Asthme

Reputation: 5353

is it possible to download images from cache memory in picasso?

I was using UniversalImageDownloader for my app.in UIL we can save images from cache memory.

 File cachedImage = ImageLoader.getInstance().getDiscCache().get(imageUrl);
            if (cachedImage.exists())
            {// code for save 2 sd
                           }

Is it possible in picasso?

Upvotes: 4

Views: 2726

Answers (4)

Hitesh Sahu
Hitesh Sahu

Reputation: 45062

Below code snippet will first make Picasso load image from cache, if failed then download and display image in imageView

You can debug memory performance with

Picasso.with(appContext).setIndicatorsEnabled(true);

  • green (memory, best performance)
  • blue (disk, good performance)
  • red (network, worst performance).

         //Debugging memory performance https://futurestud.io/tutorials/picasso-cache-indicators-logging-stats
        //TODO remove on deployment
        Picasso.with(appContext).setIndicatorsEnabled(true);
    
        //Try to load image from cache
        Picasso.with(appContext)
                .load(imageUrl)
                .networkPolicy(NetworkPolicy.OFFLINE).placeholder(R.drawable.ic_launcher)
                .placeholder(R.drawable.ic_launcher)
                .resize(100, 100)
                .error(R.drawable.ic_drawer)
                .into(markerImageView, new Callback() {
                    @Override
                    public void onSuccess() {
    
                    }
    
                    @Override
                    public void onError() {
    
                        // Try online if cache failed
                        Picasso.with(appContext)
                                .load(imageUrl)
                                .placeholder(R.drawable.ic_launcher)
                                .resize(100, 100)
                                .error(R.drawable.ic_drawer)
                                .into(markerImageView);
                    }
                });
    

Upvotes: 0

luffyjet
luffyjet

Reputation: 11

You can do like this , Use OkHttp & Picasso:

public class APP extends Application{
  public static OkHttpDownloader okHttpDownloader;

  @Override
  public void onCreate() {
    super.onCreate();
    Picasso.Builder b = new Picasso.Builder(this);
    okHttpDownloader = new OkHttpDownloader(this);
    b.downloader(okHttpDownloader);
    Picasso.setSingletonInstance(b.build());
 }
}

Then get the File from OkHttp local cache:

Downloader.Response res = APP.okHttpDownloader.load(Uri.parse(your image Url),0);
Log.i(TAG,"Get From DISK: " + res.isCached() );
storeImageFile(res.getInputStream());

Upvotes: 1

Bao Le
Bao Le

Reputation: 17497

You can get bitmap from ImangeView onSuccess() callback

Picasso.with(context).load(path).into(imageView, new Callback(){
    public void onSuccess() {
      Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
      //save bitmap to sdcard
   } 
   public void onError() {}
}

WARNING: Saved bitmap may be different to original bitmap.

Upvotes: 0

Mihir
Mihir

Reputation: 2074

There is a private Method in Picasso -

Bitmap quickMemoryCacheCheck(String key) {
    Bitmap cached = cache.get(key);
    if (cached != null) {
      stats.dispatchCacheHit();
    } else {
      stats.dispatchCacheMiss();
    }
    return cached;
  }

Modify the source according to your need.

Upvotes: 1

Related Questions