当我从图库中选择照片或从相机拍照时,我的照片在旋转?

when i choose photo from gallery or take a picture from camera my photo is rotating?

我从图库中选择照片或从相机拍摄照片,当它设置为 ImageView 时它开始旋转,我该如何修复它以使其不旋转?

 public void setNewImage() {

    new android.app.AlertDialog.Builder(getActivity())

            .setPositiveButton("camera", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    getActivity().startActivityForResult(takePicture, 0);
                }
            })

            .setNegativeButton("gallery", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    getActivity().startActivityForResult(pickPhoto, 1);
                }
            })

            .show();
}

这里我将图像设置为 imageview :

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 0:
            if (resultCode == RESULT_OK && data != null && data.getData() != null) {
                Uri filePath = data.getData();
                try {
                    //Getting the Bitmap from Gallery
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);

                    //Setting the Bitmap to ImageView
                    NewpostFragment.post_image.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }

               /* Uri picUri = data.getData();
                filePath = getPath(picUri);
                img.setImageURI(picUri);*/

            }

            break;

        case 1:
            if (resultCode == RESULT_OK && data != null && data.getData() != null) {
                /*Uri picUri = data.getData();

                filePath = getPath(picUri);

                img.setImageURI(picUri);*/



                Uri filePath = data.getData();
                try {
                    //Getting the Bitmap from Gallery
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                    //Setting the Bitmap to ImageView
                    NewpostFragment.post_image.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            break;
    }
}

首先,ACTION_IMAGE_CAPTURE 不会在 onActivityResult() 中通过 getData() 给你一个 Uri。虽然一些有问题的相机应用程序可能会这样做,但大多数不会。您的选择是:

  • ACTION_IMAGE_CAPTURE Intent 上提供 EXTRA_OUTPUT,在这种情况下,照片应存储在您指定的 Uri 所标识的位置放入 EXTRA_OUTPUT

  • 不提供EXTRA_OUTPUT,使用getParcelableExtra("data")从相机应用获取缩略图

请参阅 this sample app 了解如何将 ACTION_IMAGE_CAPTUREEXTRA_OUTPUT 一起使用。

在方向方面,如果您沿着 EXTRA_OUTPUT 路径前进,您可以 use android.support.media.ExifInterface 找出照片的方向,然后旋转图像以匹配(例如,旋转 ImageView).

请参阅 this sample app 了解如何使用 ExifInterface(尽管我使用的实现与 android.support.media.ExifInterface 不同)。