Question

I tried to make an app that allow user to select image from gallery / take picture and set the result to the bitmap. When I test the app , I found the rotation of samsung device are buggy.

After searching for a while, I found that the rotation is not defined by google but the manufactuer themselves and samsung seems have some different setting. Also, there are some suggestion to use another way to check the rotation.

How to fix the problem? (note: not only taken picture , but also the picture from gallery have the same rotation problem)

Here is the code to getbitmap from provided file path:

  private Bitmap getBitmap(String path) {

        Uri uri = getImageUri(path);
        InputStream in = null;
        try {
            in = mContentResolver.openInputStream(uri);

            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            BitmapFactory.decodeStream(in, null, o);
            in.close();

            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            in = mContentResolver.openInputStream(uri);
            Bitmap b = BitmapFactory.decodeStream(in, null, o2);
            in.close();

            return b;
        } catch (FileNotFoundException e) {
            Log.e(TAG, "file " + path + " not found");
        } catch (IOException e) {
            Log.e(TAG, "file " + path + " not found");
        }
        return null;
    }

Thanks for helping

Was it helpful?

Solution

I think what you are looking for is reading the exif rotation from an image an rotating it accordingly. I know there are problems with samsung devices that images don't face the correct way but you can correct for that like this:

First you have to read the Exif Rotation from the image:

ExifInterface exif = new ExifInterface(pathToFile);  
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); 

With this information you can correct the rotation of the image, this is unfortunately a little more complex, it involves rotating the bitmap with a matrix. You can create the matrix like this:

Matrix matrix = new Matrix();
switch (rotation) {
    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;

    case ExifInterface.ORIENTATION_NORMAL:        
    default:
        break;
}

And finally you can create the correctly rotated bitmap:

int height = bitmap.getHeight();
int width = bitmap.getWidth();
Bitmap correctlyRotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

And to avoid OutOfMemory Exceptions you should recycle the old not correctly rotated bitmap after creating the correctly rotated one like this:

bitmap.recycle();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top