3

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.

Shahryar
  • 324
  • 1
  • 3
  • 17

2 Answers2

12

Picasso holds Target instance with weak reference.
So it is better to hold Target as instance field.
see: https://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);
}
Community
  • 1
  • 1
nshmura
  • 5,940
  • 3
  • 27
  • 46
  • YESSSSSS, it is working. can you tell me more about why making a instance is so important with picasso? – Shahryar Jul 23 '16 at 13:33
  • 1
    If you don't set `Target` to instance field, `Target` will be garbage collected. For more information, Please see this issue https://github.com/square/picasso/issues/352 – nshmura Jul 23 '16 at 13:40
3

You can do like this

private Target image;
image = 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() + "/" + FILEPATH);
                    try {
                        file.createNewFile();
                        FileOutputStream outstream = new FileOutputStream(file);
                        bitmap.compress(CompressFormat.JPEG, 75, outstream);
                        outstream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
Picasso.with(this)
        .load(currentUrl)
        .into(image);
SaravInfern
  • 3,338
  • 1
  • 20
  • 44
  • look bro this is working if i want to save the bitmap. I don't want to save it. i just want to use the bitmap and save it to for example another bitmap because i want to do image effects on it. – Shahryar Jul 23 '16 at 13:12
  • do you want to cache the image and use it later?? – SaravInfern Jul 23 '16 at 13:15
  • No i just want to have it in some bitmap so i can send it to another function that is using renderscript. i want to have this: onBitmapLoaded.... otherBitmap = bitmap; imageView.setImageBitmap(bitmap); but it is not working right. try it you will see. – Shahryar Jul 23 '16 at 13:16
  • Ok then you can get it from onBitmapLoaded function what is the problem in it – SaravInfern Jul 23 '16 at 13:21
  • There is problem with setting the bitmap to imageView. Try it, it is not working correctly. I dont know why. – Shahryar Jul 23 '16 at 13:22
  • Dear @Saravlnfern see the answer from nshmura, it was because of making no instance of target. – Shahryar Jul 23 '16 at 13:32