【问题标题】:How to display Photograph without distortion in Android Application如何在 Android 应用程序中显示不失真的照片
【发布时间】:2015-06-05 11:07:56
【问题描述】:

我当前的 Android 应用程序允许用户选择他们设备上的任何照片。

我使用此代码显示所有照片以允许用户选择

final Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

选择照片后,我会将其显示在我的应用程序主活动中。

我的主要活动采用如下线性布局:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10" >

    <ImageView
        android:id="@+id/photograph"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/select_photo"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text="Photo Select" />

</LinearLayout>

我使用此代码在我的应用程序中显示照片

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri imageUri = data.getData();
            try {
                final Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }
    }
}

所选照片永远不会正确显示,例如它们或多或少地被扭曲了。

我如何保证所选照片以正确的尺寸/比例显示?

【问题讨论】:

    标签: android android-bitmap bitmapfactory


    【解决方案1】:

    将您的ImageView 调整为“正确的尺寸/比例”,或者使用android:scaleType 指明您希望将图像应用于ImageView 时的缩放方式。

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多