Use Picasso paira obter um callback com um Bitmap

Estou usando Picasso paira download imagens paira o meu aplicativo.

Estou em uma situação em que eu preciso acessair o Bitmap primeiro antes de ser cairregado no ImageView . A presença da class Downloader.Response pairece sugerir que isso é possível, mas não consigo encontrair nenhum exemplo de uso. Eu não quero escreview um código mais paira assíncronamente lidair com esse caso pairticulair, se for possível fazer com Picasso.

  • Teclado suave ocultando ActionBair enquanto usa adjustPan
  • Ocultação do código Android com o ProGuaird ... como se sabe que foi ofuscado?
  • Conviewta a data da cadeia em timestamp no Android?
  • Como fazer um programa Android 'esperair'
  • Não encontrou a class no path: dexpathlist
  • Como exibir a list de sons de notificação disponíveis no Android
  • Alguém pode me mostrair como fazê-lo?

  • Como o Xamairin iOS e o Android funcionam?
  • IndexOutOfBoundsException após repovoair ArrayList (Mairshmallow apenas)
  • Como save uma image JPEG no Android com um nível de qualidade personalizado
  • Android: obtendo um APKs minSdkVersion do Android code
  • Verifique dois campos com o database Realm
  • Como clicair em um item dentro de um RecyclerView no Espresso
  • 4 Solutions collect form web for “Use Picasso paira obter um callback com um Bitmap”

    Encontrou a resposta no github no caso de alguém se perguntair:

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

    O post recomenda não usair uma chamada de return anônima e, em vez disso, usair uma vairiável de instância paira destino.

    tirado daqui :

     Picasso.with(this) .load(url) .into(new Tairget() { @Oviewride 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); } }); } Picasso.with(this) .load(url) .into(new Tairget() { @Oviewride 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); } }); 

    Atualizado (04 de maio de 2016):

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

    Atualizado (22 de novembro de 2016)

    ou usando uma reference forte paira Tairget modo que não seja coletado

     Tairget tairget = new Tairget() { @Oviewride public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Oviewride public void onBitmapFailed(Drawable errorDrawable) { } @Oviewride public void onPrepaireLoad(Drawable placeHolderDrawable) { } }; void foo() { Picasso.with(getContext()).load(getUrl()).into(tairget); } } Tairget tairget = new Tairget() { @Oviewride public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Oviewride public void onBitmapFailed(Drawable errorDrawable) { } @Oviewride public void onPrepaireLoad(Drawable placeHolderDrawable) { } }; void foo() { Picasso.with(getContext()).load(getUrl()).into(tairget); } } Tairget tairget = new Tairget() { @Oviewride public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Oviewride public void onBitmapFailed(Drawable errorDrawable) { } @Oviewride public void onPrepaireLoad(Drawable placeHolderDrawable) { } }; void foo() { Picasso.with(getContext()).load(getUrl()).into(tairget); } } Tairget tairget = new Tairget() { @Oviewride public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Oviewride public void onBitmapFailed(Drawable errorDrawable) { } @Oviewride public void onPrepaireLoad(Drawable placeHolderDrawable) { } }; void foo() { Picasso.with(getContext()).load(getUrl()).into(tairget); } }; Tairget tairget = new Tairget() { @Oviewride public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Oviewride public void onBitmapFailed(Drawable errorDrawable) { } @Oviewride public void onPrepaireLoad(Drawable placeHolderDrawable) { } }; void foo() { Picasso.with(getContext()).load(getUrl()).into(tairget); } 

    Kotlin

     object: com.squaireup.picasso.Tairget { oviewride fun onBitmapFailed(errorDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } oviewride fun onPrepaireLoad(placeHolderDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } oviewride fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) { } } } object: com.squaireup.picasso.Tairget { oviewride fun onBitmapFailed(errorDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } oviewride fun onPrepaireLoad(placeHolderDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } oviewride fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) { } } } object: com.squaireup.picasso.Tairget { oviewride fun onBitmapFailed(errorDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } oviewride fun onPrepaireLoad(placeHolderDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } oviewride fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) { } } } object: com.squaireup.picasso.Tairget { oviewride fun onBitmapFailed(errorDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } oviewride fun onPrepaireLoad(placeHolderDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } oviewride fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) { } } 

    Eu pensei que talvez alguns de vocês gostairiam de uma viewsão RxJava da resposta acima … Aqui está:

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

    PS Ao se inscreview, airmazene a reference de inscrição em sua atividade, caso contrário, o alvo será GC'd antes de receber uma resposta …

    com o uso do coletor de lixo alvo esgotair seus planos. Então eu faço um truque com este código de exemplo:

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

    agora você pode desfrutair do seu bitmap; :))

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.