18

i'm using picasso library to download and load images into imageView. now i want to know how i can get image width and height before loading them in imageViews ?

i have a listview with an adapter that contains two imageView(one of them is vertical and another is horizontal). depends on image width and height i want to load image into one of the imageviews.

user2549089
  • 281
  • 2
  • 4
  • 14
  • Check this http://stackoverflow.com/questions/8880376/how-to-get-height-and-width-of-a-image-used-in-android – Aniruddha Aug 27 '14 at 08:56
  • You can use `int finalHeight = iv.getMeasuredHeight(); int finalWidth = iv.getMeasuredWidth();`, where `iv` is your ImageView object. – Aniruddha Aug 27 '14 at 08:58
  • @Aniruddha it just work after loading image in imageview ! but i want to know image size befor loading that – user2549089 Aug 27 '14 at 09:03
  • http://stackoverflow.com/questions/17831856/android-width-and-height-of-bitmap-without-loading-it – Aniruddha Aug 27 '14 at 09:06

2 Answers2

36

You can get Bitmap dimensions only after downloading It - you must use synchronous method call like this:

final Bitmap image = Picasso.with(this).load("http://").get();
int width = image.getWidth();
int height = image.getHeight();

After this you can call again load with same url (It will be fetched from cache):

 Picasso.with(this).load("http://").into(imageView)

Edit: Maybe better way:

 Picasso.with(this).load("http://").into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                imgView.setImageBitmap(bitmap);
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });
Yuraj
  • 3,185
  • 1
  • 23
  • 42
  • 4
    Just for completion of the answer: You cannot make this call in the Main Thread, because it is synchronos. Therefore you need to start for example an AsyncTask, which does this in the background, and comes back, when the image was loaded. – Klaus Sep 25 '14 at 08:21
  • This will work poorly with `RecyclerView` due to the asynchronous nature of picasso coupled with the fact that your `ViewHolders` will be recycled (lots of images popping into incorrect positions when scrolling through the feed because they are being loaded into their old view). – Nic Robertson Aug 26 '16 at 03:39
  • Use a second way. In first you will get: "java.lang.IllegalStateException: Method call should not happen from the main thread". – CoolMind Feb 14 '18 at 17:54
2

Work for me.

Picasso.with(context).load(imageLink).into(imageView, new Callback() {
        @Override
        public void onSuccess() {
            Picasso.with(context).load(imageLink).into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();
                    Log.d("ComeHere ", " W : "+ width+" H : "+height);
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });

        }

        @Override
        public void onError() {

        }
    });
Chattip Soontaku
  • 560
  • 1
  • 6
  • 17
  • 1
    Why would you load the image again inside the onSuccess method? – Aman Alam Oct 03 '17 at 13:27
  • Because he loads URL image 2 times, the first time he will bind image from URL, the second time he creates a fake target to get width height image. It will make a synchronized process instead of call 2 action and they are not synchronized – Lê Tấn Thành Mar 17 '20 at 09:28