Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso onBitmapLoaded never called

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

like image 632
Sanat Pandey Avatar asked Sep 22 '14 13:09

Sanat Pandey


1 Answers

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);
like image 168
Shubham Agarwal Avatar answered Sep 26 '22 23:09

Shubham Agarwal