当我从画廊选择照片或从相机拍照时我的照片正在旋转?

时间:2017-04-12 14:36:40

标签: android bitmap rotation imageview gallery

我从画廊中选择一张照片或从相机拍摄照片,当它设置为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;
    }
}

1 个答案:

答案 0 :(得分:1)

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

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

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

ACTION_IMAGE_CAPTUREEXTRA_OUTPUT一起使用,请参见this sample app

就方向而言,如果沿着EXTRA_OUTPUT路径向下走,可以use android.support.media.ExifInterface找出照片的方向,然后做一些事情来旋转图像以匹配(例如,旋转ImageView)。

请参阅this sample app了解ExifInterface(虽然我使用的是与android.support.media.ExifInterface不同的实现。)