Android - ImageView showing image from gallery in "landscape"

First, perhaps "landscape" is not the best word to describe, but I can’t think of anything else at the moment.

I use the following code to show the image in ImageView, but my image, which was taken with the device in the "portrait" (up), is shown to the side.

My code is:

mImageView = (ImageView) findViewById(R.id.iv_photo); Uri u = Uri.parse("content://media/external/images/media/7"); mImageView.setImageURI(u); 

XML:

 <ImageView android:id="@+id/iv_photo" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 

And yes, "content: // media / external / images / media / 7" is a valid path.

Any ideas?

+4
source share
2 answers

Just check the image size, if it is taller than wider then you can rotate it:
http://android-er.blogspot.com/2010/07/rotate-bitmap-image-using-matrix.html

Matching bits:

  bitmap = BitmapFactory.decodeFile(imageInSD); bmpWidth = bitmap.getWidth(); bmpHeight = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.postScale(curScale, curScale); matrix.postRotate(curRotate); Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true); myImageView.setImageBitmap(resizedBitmap); 
+2
source

 android:id="@+id/iv_photo" android:layout_width="320dip" android:layout_height="wrap_content"/> 

Use it, it can work

0
source

Source: https://habr.com/ru/post/1393232/


All Articles