"take a picture and present it" in portrait mode on Samsung Galaxy S

I am trying to simply take a picture and present it in an ImageView with my samsung galaxy s. it's working fine when I do it on landscape but not on portrait. I am not getting any error or exception - just not getting anything... There is a lot of questions about this topic and it seems to be problematic (something about camera orientation) but counldn't figure out the final solution for a simple "take a picture and present it" code. here is my (problematic) code that doesn't work:

private void setUpListeners() {
    takePicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            Log.d("onActivityResult", "CAMERA_PIC_REQUEST returned");
            dishImage = (Bitmap) data.getExtras().get("data");
            if (dishImage==null)
                Log.d("onActivityResult", "dishImage==null");
            imageView = (ImageView) findViewById(R.id.dishinfodishimageview);
            imageView.setImageBitmap(dishImage);
            imageView.setVisibility(View.VISIBLE);
            takePicture.setVisibility(View.GONE);
            (new UploadImage()).execute(null);
        }
    } else {
        Log.e("onActivityResult",
                "no able to presenting the picture of the dish");
    }

}

I just need a code that works (on any device) or a fix to my code... thx.


The reason that onCreate() is called is because when you do call the camera activity during the portrait orientation, it will change the orientation and destroy your previous activity. After finishing the onActivityResult() , your activity will be re-created.

One solution to this problem is to set the manifest to ignore changes on orientation change, you can do it by using this:

<activity android:name=".MyMainActivity"
     android:configChanges="orientation"
     android:label="@string/app_name" />

If you are using API starts with level 13, you can consider screenSize for the configChanges manifest.


I can only suggest a hack for this problem. Save your results in shared preferences in onActivityResult() and during onCreate load your content from shared preferences. I know this is a bad solution but this will keep you going until you find a better answer. And don't forget to clear your shared preferences once you are done, else your activity will always initialize with old data.


尝试下面的代码..它与三星galaxy S2我的工作在OnActivityResult()中插入以下代码

ExifInterface exif = new ExifInterface(cameraimagename);
                    String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
                    int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
                    int rotationAngle = 0;
                    System.out.println("orientation is : "+orientation);
                    System.out.println("ExifInterface.ORIENTATION_ROTATE_90 : "+ExifInterface.ORIENTATION_ROTATE_90);
                    System.out.println("ExifInterface.ORIENTATION_ROTATE_180 : "+ExifInterface.ORIENTATION_ROTATE_180);
                    System.out.println("ExifInterface.ORIENTATION_ROTATE_270 : "+ExifInterface.ORIENTATION_ROTATE_270);

                    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
                    if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
                    if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
                    System.out.println("Rotation Angle is : "+rotationAngle);
                    Matrix matrix = new Matrix();
                   // matrix.setRotate(rotationAngle, (float) photo.getWidth() / 2, (float) photo.getHeight() / 2);
                    matrix.postRotate(rotationAngle);

                    Bitmap rotatedBitmap=null;
                    try {
                        rotatedBitmap = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
链接地址: http://www.djcxy.com/p/41842.html

上一篇: Zxing与三星Galaxy Note的肖像模式问题

下一篇: 在Samsung Galaxy S上以纵向模式“拍照并呈现”