0

I implemented functional of capturing/chosing image and it works great on HTC, however, on Samsung Galaxy Note 4 (Android version is 5.1.1) it rotates image by 90 degree. Here are 2 variants of code but still rotated:

VARIANT 1:

public void captureImageCameraOrGallery() {

        Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT, null);
        galleryintent.setType("image/*");

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        Intent chooser = new Intent(Intent.ACTION_CHOOSER);
        chooser.putExtra(Intent.EXTRA_INTENT, galleryintent);
        chooser.putExtra(Intent.EXTRA_TITLE, "Select from:");

        Intent[] intentArray = { cameraIntent };
        chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
        startActivityForResult(chooser, REQUEST_PIC);
}
   public void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (requestCode == REQUEST_PIC && resultCode == RESULT_OK) {
                Uri selectedImageUri = data.getData();
                Bitmap bmp = null;
                try {
                    if (selectedImageUri != null) {
                        bmp = getBitmapFromUri(selectedImageUri);
                    }

                    if (bmp == null) {
                     return;
                    }

                    ExifInterface ei = new ExifInterface(selectedImageUri.getPath());
                    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);

                    Log.e("Capture orientation: ", String.valueOf(orientation));
                    int rotateAngle = 0;
                    switch(orientation) {

                        case ExifInterface.ORIENTATION_ROTATE_90:
                        rotateAngle = 90;
                        break;

                        case ExifInterface.ORIENTATION_ROTATE_180:
                        rotateAngle = 180;
                        break;

                        case ExifInterface.ORIENTATION_ROTATE_270:
                        rotateAngle = 270;
                        break;


                        default:
                        break;
                    }

                    bmp = rotateImage(bmp, rotateAngle);
                    mUserImage.setImageBitmap(bmp);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    }

VARIANT 2:

Using PhotoPicker lib compile 'me.iwf.photopicker:PhotoPicker:0.9.5@aar'

public void captureImageCameraOrGallery() {

        PhotoPicker.builder()
                .setPhotoCount(1)
                .setShowCamera(true)
                .setShowGif(true)
                .setPreviewEnabled(false)
                .start(this, PhotoPicker.REQUEST_CODE);
    }

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

        if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
            if (data != null) {
                ArrayList<String> photos =                       data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                Uri selectedImageUri = Uri.fromFile(new File(photos.get(0)));

                Bitmap bmp = null;
                try {
                    if (selectedImageUri != null) {
                        bmp = getBitmapFromUri(selectedImageUri);
                    }

                    if (bmp == null) {
                        return;
                    }
                    mUserImage.setImageBitmap(bmp);


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

However, it still rotating. Any help will be appreciated.

Svitlana
  • 2,938
  • 1
  • 29
  • 38

2 Answers2

1

If in your first variant you always get 0 for orientation, you can try the following. (From this post )

Try to use the information in the content cursor.

float photoRotation = 0;
boolean hasRotation = false;
String[] projection = { Images.ImageColumns.ORIENTATION };
try {
    Cursor cursor = getActivity().getContentResolver().query(photoUri, projection, null, null, null);
    if (cursor.moveToFirst()) {
        photoRotation = cursor.getInt(0);
        hasRotation = true;
    }
    cursor.close();
} catch (Exception e) {}

if (!hasRotation) {
    ExifInterface exif = new ExifInterface(photoUri.getPath());
    int exifRotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_UNDEFINED);

    switch (exifRotation) {
        case ExifInterface.ORIENTATION_ROTATE_90: {
            photoRotation = 90.0f;
            break;
        }
        case ExifInterface.ORIENTATION_ROTATE_180: {
            photoRotation = 180.0f;
            break;
        }
        case ExifInterface.ORIENTATION_ROTATE_270: {
            photoRotation = 270.0f;
            break;
        }
    }
}
Community
  • 1
  • 1
Ivan Leonenko
  • 2,363
  • 2
  • 27
  • 37
  • Sorry a bit mixed with flag !hasRotation. Does we make rotation in case if (!hasRotation) { //rotate} else { // shoululd load image without rotation here? } – Svitlana Feb 09 '17 at 00:57
  • There's a double check - first we check if content rotated using `Cursor`, if so then we set a flag and don't check exif information, if not then check exif information and get rotation data from there. – Ivan Leonenko Feb 09 '17 at 01:05
  • Ok got it thank you! And one more question should I set up smth like this: intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); when call new Intent and use mCapturedImageURI as a global variable? Does MediaStore.EXTRA_OUTPUT, has a big influence ? Or I can get it just Uri photoUri = data.getData(); in onActivityResult directly? – Svitlana Feb 09 '17 at 01:14
0

in some devices maximum in samsumg device image gets rotated by 90 degree. for that you need to check it in exif file what is the exact orientation of it and according to it you have to work.

  int rotateDegree = 0;
                try {
                    File imageFile = new File(sourcepath);
                    ExifInterface exif = new ExifInterface(
                            imageFile.getAbsolutePath());
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);

                    switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotateDegree = 270;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotateDegree = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotateDegree = 90;
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Matrix matrix = new Matrix();
        matrix.postRotate(rotateDegree );
        bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147