Can I load images from cache in picasso?

I used UniversalImageDownloader for my app.in UIL, we can save images from the cache.

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

Is this possible in picasso?

+4
source share
4 answers

Picasso has a private method -

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

Change the source to suit your needs.

+1
source

You can do this using OkHttp and 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's 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()); 
+1
source

You can get a bitmap from the 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. The saved bitmap may differ from the original bitmap.

0
source

Below the code snippet will first load the Picasso image from the cache, if this is not possible, load and show the image in imageView

You can debug memory performance with

Picasso.with (application context) .setIndicatorsEnabled (true);

  • green (memory, better 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); } }); 
0
source

Source: https://habr.com/ru/post/1499100/


All Articles