3

My app takes photos, performs some processing, and ensures they are included in the Android gallery.

The problem I have is that when I open the images in the gallery they aren't rotated correctly - although examining the Exif data shows that the orientation tag is set and appears to be correct. If I load it into an Exif examination tool it will say something like "Orientation: Rotate 90 CW" but I notice that other applications, such as Gimp, also appear to miss the Exif data so I am wondering whether there is something wrong with the data as I am storing it.

My Exif orientation code is written like this:

    private void writeOrientation(int orientation, String path) {
        try {
            int exifOrientation = ExifInterface.ORIENTATION_NORMAL;
            if (45 < orientation) {
                if (orientation <= 135) {
                    exifOrientation = ExifInterface.ORIENTATION_ROTATE_90;
                } else if (orientation <= 225) {
                    exifOrientation = ExifInterface.ORIENTATION_ROTATE_180;
                } else {
                    exifOrientation = ExifInterface.ORIENTATION_ROTATE_270;
                }
            }
            ExifInterface exif = new ExifInterface(path);
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(exifOrientation));
            exif.saveAttributes();
        } catch (Exception e) {
            Log.w(TAG, "Failing to write orientation: " + e.getMessage());
            Log.getStackTraceString(e);
        }
    }

This does appear to be working- in my own code I'm rotating the image according to its orientation and it looks normal. But in the gallery, all my portrait images are still showing as landscape.

Does the gallery use the Exif data to rotate images? If not is there something else that it does use or should I just be overwriting the image with the rotated version? How can I make sure that the gallery and other apps that my images are loaded into have everything the need to display them in the correct orientation?

glenatron
  • 11,018
  • 13
  • 64
  • 112

2 Answers2

1

Nice question.

If orientation is saved into file but doesn't appear in the gallery it may be because of orientation is cached in MediaStore. So you need to try to update this information there also.

Ref - https://stackoverflow.com/a/22371344/9640177

With that I would say that some apps may not be implementing Exif Interface and in that case it would be best to override the image with rotated version.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
1

The disappointing solution in my case is that I am a massive idiot. I was loading the images in, using the Exif data to rotate them, updating them and saving them with the Exif data preserved. The part of this process that I missed, was that I was saving them rotated, so portrait images were now in the correct orientation without rotation, then the Exif data was instructing the gallery to rotate them.

Sometimes ( almost always ) the problem is the system doing exactly what you instructed it to.

glenatron
  • 11,018
  • 13
  • 64
  • 112