Android - 从图库中显示“横向”图片的ImageView

5

首先,也许“景观”不是最好的词来描述,但我暂时想不到其他的词。

我正在使用以下代码在ImageView中显示图像,但是我的用设备拍摄的“纵向”(头朝上)的图像显示成了横向。

我的代码:

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"/>

是的,“content://media/external/images/media/7” 是一个有效的路径。有什么想法吗?

是的。事实证明,我的设备只能横向拍照。 这个“bug”在键盘和椅子之间。 :( - Raist
2个回答

3

只需检查图像的尺寸,如果它比宽度更高,则可以将其旋转:
http://android-er.blogspot.com/2010/07/rotate-bitmap-image-using-matrix.html

相关部分如下:

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

curScale和curRotate是什么? - Bugs Happen
对于那些无法理解这个答案的人,这里有一个更详细解释的相同内容。https://dev59.com/gGPVa4cB1Zd3GeqP3CdY#10332901 - Bugs Happen

0

android:id="@+id/iv_photo"

android:layout_width="320dip"   

android:layout_height="wrap_content"/>

Use this it may works


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接