Matisse: Images are rotated in Samsung Devices.

Created on 11 Oct 2018  ·  2Comments  ·  Source: zhihu/Matisse

Describe the bug
Images taken by *Samsung Android 7.0 are rotated*.

Expected behavior
I tried others phones and tablet, it is OK, mostly happen in Samsung Devices.Mostly happen in portrait mode.Landscape mode is correct( tested with Galaxy Tab S 8.4 ), or not sure other models.

Screenshots
I attached the Gif for your reference.
Samsung Error

Smartphone (please complete the following information):

  • Device: [Samsung J7 Pro]
  • OS: [Android 7.0]
  • Version [API level 24]

Most helpful comment

Thanks @REBOOTERS , I have been fixed and here is the solutions if someone needs help for that problem.

imgPicked.setVisibility(View.INVISIBLE);
ExifInterface exif = null;
                try {
                    exif = new ExifInterface(realPath);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);


                Observable.fromCallable(() -> getFixBitMapFromFile(realPath, orientation))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<Bitmap>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Bitmap bitmap) {
                                imgProgressBar.setVisibility(View.INVISIBLE);
                               //resized and save bitmap to a directory
                                saveImage(bitmap);
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });


private Bitmap getFixBitMapFromFile(String filePath, int orientation) {
        File image = new File(filePath);
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);

        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }

All 2 comments

Images taken by some Samsung Android device are rotated .
This condition seems like a bug or some kind of feature which caused by Samsung. there're lots of questions and answers about this problem on stack overflow . While ,it's better to compatible this issue in your business by yourself . Matisse just want to return the image's path which captured .

Thanks @REBOOTERS , I have been fixed and here is the solutions if someone needs help for that problem.

imgPicked.setVisibility(View.INVISIBLE);
ExifInterface exif = null;
                try {
                    exif = new ExifInterface(realPath);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);


                Observable.fromCallable(() -> getFixBitMapFromFile(realPath, orientation))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<Bitmap>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Bitmap bitmap) {
                                imgProgressBar.setVisibility(View.INVISIBLE);
                               //resized and save bitmap to a directory
                                saveImage(bitmap);
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });


private Bitmap getFixBitMapFromFile(String filePath, int orientation) {
        File image = new File(filePath);
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);

        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
Was this page helpful?
0 / 5 - 0 ratings