简体   繁体   中英

Android Picasso get image bitmap from cache how

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:

  • Handling ImageView recycling and download cancelation in an adapter.
  • Complex image transformations with minimal memory use.
  • Automatic memory and disk caching . . .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM