How to retrieve images from cache memory in picasso?
Asked Answered
B

2

5

I am using picasso library for loading images .In default picasso, It uses internal cache memory for loading images.But as per my app configuration ,i have to use external cache memory(Cache on Disk). so i used this code for Cache on Disk

              File httpCacheDir = new File(getApplicationContext().getExternalCacheDir(),"http");
                long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
               HttpResponseCache.install(httpCacheDir, httpCacheSize);}

Picasso is flexible. So now it caches images in external Sd card..

The caches is stored in sdcard/android/data/packagename/cache/http The caches are stored in ".1" ,".0". formats so i just opened them and changes into ".1" to ".jpg".it gives exact images what i need. But how to do in programatically? but picasso itself caches my memory in to my app for loading image into imageview.but i have to save them into sdcard directly as images/set bitmap as wallpaper in offline mode?

Beslobber answered 28/8, 2013 at 10:36 Comment(3)
if you have got the file path and you are pretty sure that file stores end in either .0 or .1 then just get the file name and replace the 0 or 1 with the jpgVictoriavictorian
@Usman Kurd The file name look like encrypted in Md5 hash..above 40 words .Beslobber
@AndroSelva for saving them into sdcard without downloading again or set as wallpaper directly..Beslobber
S
5

You can supply your own Cache implementation when building your Picasso instance. This way you can provide extra methods that you can call to retrieve bitmaps directly from your memory cache. Use Picasso.Builder to provide your own implementation for it. When you use with() you are using a static singleton internal instance thats setup with most of the default values (most apps need the default values anyway.)

Keep a reference of your Cache implementation around and directly interact with it. Picasso is meant to handle the loading/decoding and caching for you but there is no reason you cant build around it.

If you are referring about the disk cache, then no Picasso does not support that at the moment. This is by design because the disk layer cache is done by the HTTP layer and makes no distinction about it.

You could however, change the path of the disk cache. If you are using OkHttpDownloader then supply a different file when you construct your Downloader. Similarly for UrlConnectionDownloader you could extend it and override the load() method.

Snuggery answered 31/8, 2013 at 20:33 Comment(7)
Hey my question was wrong.sdcard memory/internal memory it's not a problem,I don't want to access cache externally/directly.i just want to access from locally from picasso.when ever the image is loaded into imageview, i just want to save the images into sd card . is it possible?Beslobber
So let me get this straight, you are trying to CHANGE where the files are stored, instead of being internal you want them to be stored in the sdcard? You can either use OkHttpDownloader and when you create it pass in the cacheDir you want, or if you use UrlConnectionDownloader extend it and implement the load() method similar to what the base class does. I am updating my response.Snuggery
I'm making online gallery app which has more than 1000 pics.The app features are "set as wallpaper in home screen" and "save to SD card". whenever the image is loaded into imageview,the user can instantly "set it as wallpaper" or "save 2 SD card".hence i want to access bitmap from cache memory or any other method.but creating new cache memory and retrieve it's very hard for me.i would like to access inner side of the Picasso api. I've been trying for a week.Pls help me out.Beslobber
But YOU provide the Cache implementation for memory and YOU can provide the File for the DISK cache access. If you pass those into Picasso there is no reason why you cant keep them around to access bitmaps for specific URLs. I don't see the problem here, you can also use Picassos get() method to synchronously load a bitmap from the cache (memory or disk) using the same key and save it to the SD card.Snuggery
I have tried some thing like this ,Bitmap b=cache.get(Url). but its not working.i dont know how to get the key value.Thanx for ur responseBeslobber
@Snuggery i find the key is some thing like http://dfdafasdf.jpg resize:214x214 , not the urlTench
Here is a gist which you can use to get inspiration from - gist.github.com/heitara/322f4d5e9484ca1b9ec8Hollowell
V
1

Picasso does handle the caching in it and downloading also you just need to place it in your target Image view similar to Aquery

According to The Corner Square Engineering blog picasso handle downloading caching in it self and give its handler to user to use it and place the image in image view

Victoriavictorian answered 28/8, 2013 at 11:46 Comment(1)
U r right.u can use external disk cache .picasso is also flexible.picasso itself creating caches in ExtenalsdCard and give its handle to user to use and place the image in imageview.but i want to save them in sdcard asimges from cache memory..Beslobber

© 2022 - 2024 — McMap. All rights reserved.