Picasso onBitmapLoaded never called

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I am having the same problem, that I want to use the drawables that are generated by the Picasso for Image Caching Purpose, but I am not able to get the same. Here is the code which I am using to access the Bitmap Drawables :

Target targetBitmap = new Target() {              @Override             public void onPrepareLoad(Drawable arg0) {              }              @Override             public void onBitmapLoaded(Bitmap arg0, Picasso.LoadedFrom arg1) {                 mBitmap = arg0;                 BitmapDrawable d = new BitmapDrawable(context.getResources(), arg0);                  int margin = 2;                 int border = 0;                  Rect r = new Rect(margin, margin, width - margin, height - margin);                  int imageWidth = r.width();                 int imageHeight = r.height();                  if (imageHeight > r.height() - (border * 2)) {                     imageHeight = r.height() - (border * 2);                 }                  r.left += ((r.width() - imageWidth) / 2) - border;                 r.right = r.left + imageWidth + border + border;                 r.top += ((r.height() - imageHeight) / 2) + border;                 r.bottom = r.top + imageHeight + border + border;                  Paint p = new Paint();                 p.setColor(0xFFFFFF);                  c.drawRect(r, p);                 r.left += border;                 r.right -= border;                 r.top += border;                 r.bottom -= border;                 d.setBounds(r);                 d.draw(c);             }              @Override             public void onBitmapFailed(Drawable arg0) {              }         };          Picasso.with(context).load(app.getListBookDetails().get(img_num).getImage_150x225()).into(targetBitmap); 

But here onBitmapLoaded method is never called. How can I access this method ? Please help if you can. I have scratched everything but has gone all in vain.

Thanks Sanat

回答1:

Here is the sample code in case you want to prevent Target object from being garbage collected:

 final Target mTarget = new Target() {                         @Override                         public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {                             Log.d("DEBUG", "onBitmapLoaded");                             BitmapDrawable mBitmapDrawable = new BitmapDrawable(getResources(), bitmap);                             navigationMenuItem.setIcon(mBitmapDrawable);                         }                          @Override                         public void onBitmapFailed(Drawable drawable) {                             Log.d("DEBUG", "onBitmapFailed");                         }                          @Override                         public void onPrepareLoad(Drawable drawable) {                             Log.d("DEBUG", "onPrepareLoad");                         }                     };                       Picasso.with(this).load(tempUrl).into(mTarget); 


回答2:

In order to get Bitmap use next with RxJava 2:

fun getBitmapSingle(picasso: Picasso, url: String): Single<Bitmap> = Single.create {     try {         if (!it.isDisposed) {             val bitmap: Bitmap = picasso.load(url).get()             it.onSuccess(bitmap)         }     } catch (e: Throwable) {         it.onError(e)     } } 

Retrieve Bitmap:

getBitmapSingle(Picasso.with(context), "https:/...")                 .subscribeOn(Schedulers.io())                 .observeOn(AndroidSchedulers.mainThread())                 .subscribe({ bitmap ->                 // val drawable = BitmapDrawable(context, bitmap)                 }, Throwable::printStackTrace) 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!