0

I am trying to display gallery images in a gridview, and on image click, open a fullscreen view for the image. The weird thing is, that when the images are displaying in the gallery, they follow the correct rotation. But When I simply query the photos in my custom gallery, they are always rotated by 90 degrees counter clockwise.

I have seen some solutions to rotate using exif interface which will require memory to rotate since it loads the original bitmap and the newly rotated one.

My question is: Why does simply displaying an image from the gallery in an ImageView result in the image being rotated?

Thanks.

Pacemaker
  • 1,117
  • 2
  • 17
  • 36
  • this thing is happening because height and width of your selected image is not in proportion to show in its original position – Android is everything for me Jul 24 '14 at 10:02
  • Okay I suspected that this is the case. But the problem is that each image has different height and width. So letting them take their respected size in a grid view will make the grid view look irregular. Do you have any suggestions to make it look symmetric and show the correct rotation at the same time? – Pacemaker Jul 24 '14 at 10:31

2 Answers2

1

First you need to create an ExifInterface:

ExifInterface exif = new ExifInterface(filename);

You can then grab the orientation of the image:

orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

Here's what the orientation values mean: http://sylvana.net/jpegcrop/exif_orientation.html

So, the most important values are 3, 6 and 8. If the orientation is 6, for example, you can rotate the image like this:

 Matrix matrix = new Matrix();
matrix.postRotate(90);
rotatedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(),       sourceBitmap.getHeight(), matrix, true);
  • 1
    Hi, Thanks for the solution. But I am trying to avoid loading two bitmaps as this will give me an OutOfMemoryError. – Pacemaker Jul 24 '14 at 10:33
1

Try like this you can avoid OOM Error refer Load Bitmap Effieiently and Whatsapp bitmap loading

public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {

                final int halfHeight = height / 2;
                final int halfWidth = width / 2;

                // Calculate the largest inSampleSize value that is a power of 2 and
                // keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > reqHeight
                        && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }
            }
            return inSampleSize;
        }


    public static Bitmap decodeSampledBitmapFromResource(Resources res,
                int resId, int reqWidth, int reqHeight) {
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(res, resId, options);
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeResource(res, resId, options);
        }

Check the rotation of the Image and display it properly...as

    scaledBitmap=decodeSampledBitmapFromResource(.....);
    ExifInterface exif=null;
            try {
                exif = new ExifInterface(filePath);
                int orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION, 0);
                Log.d("EXIF", "Exif: " + orientation);
                Matrix matrix = new Matrix();
                if (orientation == 6) {
                    matrix.postRotate(90);
                    Log.d("EXIF", "Exif: " + orientation);
                } else if (orientation == 3) {
                    matrix.postRotate(180);
                    Log.d("EXIF", "Exif: " + orientation);
                } else if (orientation == 8) {
                    matrix.postRotate(270);
                    Log.d("EXIF", "Exif: " + orientation);
                }
                scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                        scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
                        true);
            } catch (IOException e) {
                e.printStackTrace();
            }
Santhosh
  • 1,867
  • 2
  • 16
  • 23
  • Okay this looks like a nice solution. As far as I can see, it does not require loading the original bitmap, correct? Because if it does this will probably give me an OutOfMemoryError. – Pacemaker Jul 24 '14 at 10:32
  • The line : scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); has a mistake, shouldn't the first parameter be the original bitmap? – Pacemaker Jul 24 '14 at 10:34
  • Okay thanks, I will try with those and get back to you. – Pacemaker Jul 24 '14 at 10:58