从相机和画廊获取照片

时间:2013-02-26 00:03:36

标签: android android-camera android-camera-intent

您好,

我正在创建一个简单的Android应用程序,允许用户从任一照片库中选择照片或使用相机拍摄照片。获得所选照片后,我将开始另一项活动来对照片做些什么。但是我在另一项活动中显示照片时遇到了一些问题。

这是第一个获得照片的活动:

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);
        }
    }
}

然后我开始另一项活动(PhotoProcessing),在此活动中,我将首先显示照片。如果我使用相机拍照,就会出现问题,它以纵向模式显示,这就是我想要的。但是,如果我从图库中选择一张照片,它总是水平显示,这真的很烦人。有谁知道如何解决这个问题?

当我试图获得图像的宽度和高度时:

float imageWidth = GetPhoto.bmp.getWidth();
float imageHeight = GetPhoto.bmp.getHeight();
  • 如果照片是从相机拍摄的,imageWidth = 153imageHeight = 204
  • 如果照片是从图库中选择的,imageWidth = 3264imageHeight = 2448

为同一张图片。

任何人都能解释为什么会这么不同吗?我很困惑。

任何解决方案或建议将不胜感激。感谢。

0 个答案:

没有答案