Skip to content Skip to sidebar Skip to footer

Downloading Images With Picasso Android Disck

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()).into(holder.image)

Solution 1:

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();
    }
}

Post a Comment for "Downloading Images With Picasso Android Disck"