Shahryar Shahryar - 4 months ago 135
Android Question

Load Image With Picasso to a bitmap first

I'm using Picasso.
And i want to add the image to bitmap first and then add it to an imageview. I'm using the following line of code that adds an image from gallery with uri and show it on image view. I want to save it on a bitmap first. what should i do:

Picasso.with(this).load(uriadress).into(imageView);


but i want to save it on a bitmap first.

Answer

Picasso holds Target instance with weak reference.
So it is better to hold Target as instance field.
see: http://stackoverflow.com/a/29274669/5183999

private Target mTarget;

void loadImage(Context context, String url) {

    final ImageView imageView = (ImageView) findViewById(R.id.image);

    mTarget = new Target() {
        @Override
        public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
            //Do something
            ...

            imageView.setImageBitmap(bitmap);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };

    Picasso.with(context)
            .load(url)
            .into(mTarget);
}