javaandroidimage-captureimagelibrary

Image captured or picking from imagelibrary rotated photo by 90 degree


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.


Solution

  • 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;
            }
        }
    }