【问题标题】:when i choose photo from gallery or take a picture from camera my photo is rotating?当我从图库中选择照片或从相机拍照时,我的照片正在旋转?
【发布时间】:2017-09-08 10:02:03
【问题描述】:

我从图库中选择照片或从相机拍照,当它设置为 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;
    }
}

【问题讨论】:

    标签: android bitmap rotation imageview gallery


    【解决方案1】:

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

    • ACTION_IMAGE_CAPTUREIntent上提供EXTRA_OUTPUT,在这种情况下,照片应存储在您放入EXTRA_OUTPUTUri标识的位置,或者

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

    请参阅this sample app 以将ACTION_IMAGE_CAPTUREEXTRA_OUTPUT 结合使用。

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

    有关使用 ExifInterface 的信息,请参阅 this sample app(尽管我使用的实现与 android.support.media.ExifInterface 不同)。

    【讨论】:

      猜你喜欢
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2016-08-18
      • 2015-10-20
      • 1970-01-01
      • 2016-09-06
      相关资源
      最近更新 更多