My current Android Application allows the user to select any photograph they have on their device.
I use this code to display all photographs to allow user selection
final Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
Once the photograph has been selected, i display it within my applications main activity.
My main activity employs a LinearLayout as follows:-
<?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>
I use this code to display the photograph in my application
@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();
}
}
}
}
The selected photographs are never dislayed correctly, e.g. they are distorted to a lesser or greater degree.
How do i guarantee that any photograph selected is displayed with the correct dimensions/proportions?