getting image width and height with picasso library

无人久伴 提交于 2019-11-27 20:40:08

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

            }
        });

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

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