Use Picasso to Get Callback Using Bitmap

I use Picasso to upload images for my application.

I am in a situation where I need to access Bitmap first before it loads in ImageView . The presence of the Downloader.Response class seems to suggest that this is possible, but I cannot find any usage examples. I donโ€™t want to write a bunch of more code to asynchronously handle this particular case, if possible with Picasso.

Can someone show me how to do this?

+65
android bitmap picasso
Nov 24 '13 at 21:51
source share
4 answers

Found an answer on github if someone wonders:

 private Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } } private void someMethod() { Picasso.with(this).load("url").into(target); } @Override public void onDestroy() { // could be in onPause or onStop Picasso.with(this).cancelRequest(target); super.onDestroy(); } 

The message recommends not using an anonymous callback and instead using an instance variable for the purpose.

+145
Nov 24 '13 at 22:02
source share

taken from here :

 Picasso.with(this) .load(url) .into(new Target() { @Override public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){ /* Save the bitmap or do something with it here */ //Set it in the ImageView theView.setImageBitmap(bitmap); } }); 

Updated (May 04, 2016):

  Picasso.with(this) .load(youUrl) .into(new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }); 

Updated (November 22, 2016)

or using a strong link for Target so that it does not collect garbage

 Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }; void foo() { Picasso.with(getContext()).load(getUrl()).into(target); } 

Kotlin

 object: com.squareup.picasso.Target { override fun onBitmapFailed(errorDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onPrepareLoad(placeHolderDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) { } } 
+28
Dec 21 '15 at 7:43
source share

I thought maybe some of you would like to get a version of RxJava from the answer above ... Here it is:

  public static Observable<Bitmap> loadBitmap(Picasso picasso, String imageUrl) { return Observable.create(new Observable.OnSubscribe<Bitmap>() { @Override public void call(Subscriber<? super Bitmap> subscriber) { Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { subscriber.onNext(bitmap); subscriber.onCompleted(); } @Override public void onBitmapFailed(Drawable errorDrawable) { subscriber.onError(new Exception("failed to load " + imageUrl)); } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }; subscriber.add(new Subscription() { private boolean unSubscribed; @Override public void unsubscribe() { picasso.cancelRequest(target); unSubscribed = true; } @Override public boolean isUnsubscribed() { return unSubscribed; } }); picasso.load(imageUrl).into(target); } }); } 

PS When subscribing, save the link to subscribe to your activity, otherwise the goal will be GC'd before you get an answer ...

+2
Feb 20 '16 at 23:59
source share

Use your targeted garbage collector to exhaust your plans. So I do the trick with this code example:

 ImageView bitmapImageView = new ImageView(context); picasso.with(context).load(url).into(bitmapImageView); Bitmap bitmap = ((BitmapDrawable)bitmapImageView.getDrawable()).getBitmap(); 

Now you can enjoy your bitmap; :))

-5
Sep 05 '15 at 15:22
source share



All Articles