Qadir Hussain
Qadir Hussain

Reputation: 8856

How to get the size of bitmap after displaying it in ImageView

I have a imageview

<ImageView
        android:id="@+id/imgCaptured"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/captured_image" />

I capture a image from camera, converted that image into bitmap.

Bitmap thumbnail;
thumbnail = MediaStore.Images.Media.getBitmap(getActivity()
                    .getContentResolver(), imageUri);

when i get the resolution of this bitmap before displaying it in my above imageview, like

Log.i("ImageWidth = " + thumbnail.getWidth(), "ImageHeight = "
                + thumbnail.getHeight());

its returning me ImageWidth = 2592 ImageHeight = 1936

after this i displayed this bitmap in my above imageview as imgCaptured.setImageBitmap(thumbnail); then i go size of my imageview as

Log.i("ImageView Width = " + imgCaptured.getWidth(),
                "ImageView Height = " + imgCaptured.getHeight());

this returned me ImageView Width = 480 ImageView Height = 720

now my question is that

Edit

Actually i have captured an image of 2592x1936. I displayed this image in my imageView, did some other operations on this image . now i want to save this image with same 2592x1936 resolution. is it possible?

Thanks in advance.

Upvotes: 8

Views: 17983

Answers (1)

Tang Ke
Tang Ke

Reputation: 1508

After you display a Bitmap in a ImageView, The ImageView will create a BitmapDrawable object to draw it in ImageView's Canvas. So you can invoke ImageView.getDrawable() method to get the reference of the BitmapDrawable, and get the Bounds by invoke Drawable.getBounds(Rect rect) method. through the bounds, you can compute the width and height of the Bitmap drawn in ImageView

Drawable drawable = ImageView.getDrawable();
//you should call after the bitmap drawn
Rect bounds = drawable.getBounds();
int width = bounds.width();
int height = bounds.height();
int bitmapWidth = drawable.getIntrinsicWidth(); //this is the bitmap's width
int bitmapHeight = drawable.getIntrinsicHeight(); //this is the bitmap's height

Upvotes: 13

Related Questions