I didn't find any proper example of how to retrieve a bitmap image from cache picasso:
Here is the code where I download an imageview with Picasso. I need the bitmap image. How to take it from cache?
MWGApp.getInjector().getImageLoader().load(url)
.placeholder(ResourcesCompat.getDrawable(context.getResources(), R.drawable.image_coming_soon, context.getTheme()))
.into(imageVoew);
}
From official website you can see that Picasso
has features like:
And to get Bitmap
from Picasso
you can set your code:
Picasso.with(this)
.load(youUrl)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// here is your bitmap use it
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
public static Bitmap getBitmap(Context context, String url)
{
final String CACHE_PATH = context.getCacheDir().getAbsolutePath() + "/picasso-cache/";
File[] files=new File(CACHE_PATH).listFiles();
for (File file:files)
{
String fname= file.getName();
if (fname.contains(".") && fname.substring(fname.lastIndexOf(".")).equals(".0"))
{
try
{
BufferedReader br=new BufferedReader(new FileReader(file));
if (br.readLine().equals(url))
{
String image_path= CACHE_PATH + fname.replace(".0", ".1");
if (new File(image_path).exists())
{
return BitmapFactory.decodeFile(image_path);
}
}
}
catch (FileNotFoundException|IOException e)
{
}
}
}
return null;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.