问题
Hi,
I am creating a simple Android app which allows user to select photo from either photo gallery or take one using camera. After getting the selected photo, I will start another activity to do something with the photo. However I have some problem displaying the photo in another activity.
Here is the first activity to get photo:
public class GetPhoto extends Activity implements OnClickListener {
ImageButton ibPhotoLib;
ImageButton ibCamera;
final static int FROM_CAMERA = 0, FROM_PHOTOLIB = 1;
public static Bitmap bmp;
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.ibCamera:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, FROM_CAMERA);
break;
case R.id.ibPhotoLib:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(photoPickerIntent, FROM_PHOTOLIB);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case FROM_CAMERA:
Bundle extras = data.getExtras();
bmp = (Bitmap)extras.get("data");
break;
case FROM_PHOTOLIB:
Uri chosenImageUri = data.getData();
try {
bmp = Media.getBitmap(this.getContentResolver(), chosenImageUri);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
Intent intent = new Intent(GetPhoto.this, PhotoProcessing.class);
startActivity(intent);
}
}
}
Then I start another activity (PhotoProcessing
) and in this activity I will display the photo first. Here comes the problem if I take a photo using camera, it displays in portrait mode which is what I want. However if I choose a photo from gallery, it always displays horizontally, which is really annoying. Does anyone know how to solve this problem?
And also when I try to get the width and height of the image:
float imageWidth = GetPhoto.bmp.getWidth();
float imageHeight = GetPhoto.bmp.getHeight();
- if the photo is taken from camera,
imageWidth
=153
,imageHeight
=204
- if the photo is chosen from gallery,
imageWidth
=3264
,imageHeight
=2448
for the same image.
Can anyone explain why this is so different? I am very confused.
Any solutions or suggestions will be appreciated. Thanks.
回答1:
The bitmap passed back from a camera intent is a thumbnail. Even worse, I think some manufacturers' camera implementations don't return anything.
You'll need to give the camera intent a file and then fetch the image data from the file in your onActivityResult method.
Check out the Taking Photos Simply guide for implementation details.
来源:https://stackoverflow.com/questions/15078955/getting-photo-from-camera-and-gallery