Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting image width and height with picasso library

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.

like image 511
user2549089 Avatar asked Aug 27 '14 08:08

user2549089


People also ask

How will you load an image into an imageView from an image URL using Picasso and display a custom image if there is an error while loading the image?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details.

How do I set the width of an image in Picasso?

Add an explicit width or height to the ImageView by setting layout_width=500dp in the layout file and then be sure to call fit () during your load: Picasso.with (…).load (imageUri).fit ().into (…) Open up your static placeholder or error images and make sure their dimensions are relatively small (< 500px width).

How to load an image into an image view using Picasso?

In the first line, we get a reference to the ImageView instance in the layout file. We then load an image into the image view using the Picasso library. We first specify the context by calling with () and passing in the context. We then call the load () method and supply in it with the location of the image.

What is Picasso library for Android?

It is among the powerful image download and caching library for Android. Picasso simplifies the process of loading images from external URLs and displays them on your application. For example, downloading an image from the server is one of the most common tasks in any application.

What is the advantage of using fit() over image in Picasso?

First, calling fit () can delay the image request since Picasso will need to wait until the size of the ImageView can be measured. Second, you only can use fit () with an ImageView as the target (we'll look at other targets later). The advantage is that the image is at the lowest possible resolution, without affecting its quality.


2 Answers

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) {

            }
        });
like image 147
Yuraj Avatar answered Nov 28 '22 10:11

Yuraj


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() {

        }
    });
like image 41
Chattip Soontaku Avatar answered Nov 28 '22 10:11

Chattip Soontaku