我当前的Android应用程序允许用户选择他们设备上的任何照片。
我使用这段代码来显示所有的照片,以允许用户选择
final Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
一旦照片被选中,我将它显示在我的应用程序主活动中。
我的主要活动使用LinearLayout如下:-
<?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();
}
}
}
}
所选照片从未正确显示,例如,它们或多或少地扭曲了。
我如何保证所选的照片以正确的尺寸/比例显示?
将ImageView
的大小调整为"正确的尺寸/比例",或者使用android:scaleType
来指示您希望图像在应用于ImageView
时如何缩放