图像自动旋转与android中的图像视图

时间:2015-03-07 04:46:23

标签: android imageview

我正在尝试从图库中选择图像并设置为imageview。图像已设置,但图像会自动设置为水平视图。我想设置垂直视图。

我的代码是:

public void edit_profile_pic(View view) 
{
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

 @Override
 protected void onActivityResult(int requestCode, int resultCode,   Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            System.out.println(selectedImage);
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
            ImageView imgView = (ImageView) findViewById(R.id.profile_profile_picture);
            imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));

        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
       e.printStackTrace();
    }

}

Xml文件:

  <ImageView
      android:id="@+id/profile_profile_picture"
      android:layout_width="300dp"
      android:layout_height="300dp"
      android:contentDescription="@string/profile_picture"
      android:layout_marginTop="@dimen/margin_components"
      android:src="@drawable/profile_picture"        
      android:layout_gravity="center_horizontal"
      android:scaleType="centerCrop"
      android:onClick="edit_profile_pic" />

1 个答案:

答案 0 :(得分:0)

我认为您选择的图像是风景图像。尝试选择肖像图像,您将获得垂直视图。

如果您想要在垂直视图中显示所有图像,则需要为ImageView控件提供自定义宽度和高度。

还在imageview控件上设置android:scaleType="centerCrop"

相关问题