downloading images with Picasso android disck

后端 未结 1 2002
孤独总比滥情好
孤独总比滥情好 2021-01-25 21:46

I\'m using the Picasso library to download and display images in a listview, I\'m using the following code:

Picasso.with(mContext).load(listItem.getMainPhoto(         


        
相关标签:
1条回答
  • 2021-01-25 22:25

    i know this is a old question but maybe someone can find this useful.

    you can download an image with picasso using a target:

        Picasso.with(mContext)
        .load(listItem.getMainPhoto())
        .into(target);
    
    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {               
    
                    File file = new File(Environment.getExternalStorageDirectory().getPath() +"/imagename.jpg");
                    try
                    {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(CompressFormat.JPEG, 75, ostream);
                        ostream.close();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
    
                }
            }).start();
        }
        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }
    
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            if (placeHolderDrawable != null) {
            }
        }
    };
    

    To clean the cache you can add this class to the picasso package:

    package com.squareup.picasso;
    
    public class PicassoTools {
    
        public static void clearCache (Picasso p) {
            p.cache.clear();
        }
    }
    
    0 讨论(0)
提交回复
热议问题