3

I'm trying to process images in my app. The problem I'm currently facing is related to orientation of images. The thumbnail of the images selected from camera folder of Android appears 90 degrees rotated. I'm getting the thumbnail as following;

    Uri thumbUri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, uri.getLastPathSegment());
    if (thumbUri != null){
        try {
            List<String> parts = uri.getPathSegments();
            String lastPart = parts.get(parts.size() - 1);
            int index = lastPart.indexOf(":");
            if (index != -1)
            {
                lastPart = lastPart.substring(index+1);
            }
            long id = Long.parseLong(lastPart);

            // get a thumbnail for the image
            Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
                    context.getContentResolver(),
                    id,
                    MediaStore.Images.Thumbnails.MINI_KIND,
                    null
            );
            if (bitmap != null)
            {
                return bitmap;
            }
        }
        catch (Exception e)
        {
            Log.e(LOG_TAG, "Unable to generate thumbnail from thumbnail uri " + e.getMessage(), e);
        }

    }

Also tried to fix it by reading orientation from ExifInterface as;

ExifInterface ei = new ExifInterface(uri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

But orientation returned is always 0 ORIENTATION_UNDEFINED. Unable to get what I'm doing wrong here.

Ammar
  • 1,811
  • 5
  • 26
  • 60
  • Take a look at these two links: [Android: Bitmaps loaded from gallery are rotated in ImageView](http://stackoverflow.com/questions/3647993/android-bitmaps-loaded-from-gallery-are-rotated-in-imageview) and [Camera preview is in portrait mode but image captured is rotated](http://stackoverflow.com/questions/16128608/camera-preview-is-in-portrait-mode-but-image-captured-is-rotated) – Hana Bzh Jan 03 '15 at 13:04
  • @HBizhi `ExifInterface` was not helpful from me as I already mentioded in question. – Ammar Jan 03 '15 at 13:14

2 Answers2

2

Got the solution. I used getThumbnail(ContentResolver contentResolver, long id) method from BitmapUtil which reads the metadata of image from cursor and then process accordingly.

Thanks to Jason Fry for this useful utility.

Ammar
  • 1,811
  • 5
  • 26
  • 60
1

You can do a simple workaround by performing additional rotation to your bitmap, using Matrix. It's perfectly simple.

//we don't need a NullPointerException now, do we?
if (bitmap != null)
{
    //ever so simply initialize a Matrix
    Matrix matrix = new Matrix();

    //tell the Matrix it should rotate everything it's applied to by -90 degreeds
    matrix.postRotate(-90);

    //create a clone of the bitmap while applying the mentioned Matrix
    //for width and height attributes, you must always use old bitmap's width and height
    bitmap = Bitmap.createBitmap(bitmap, 0, 0,
                                 bitmap.getWidth(), bitmap.getHeight(), 
                                 matrix, true);

    return bitmap;
}

EDIT:

And if you need to determine which orientation (landscape/portrait) were the photos taken in, and then decide which ones to rotate, you can use a code from this question/answer, with ExifInterface. I see you've already applied something similar, so maybe you should try this modification with only 1 parameter, as in the linked answer:

exif.getAttribute(ExifInterface.TAG_ORIENTATION);

UPDATE:

OK, so it doesn't work either. But it should, right? That's what the mentioned method does. So I suggest you check out what uri.getPath() actually returns (using a debugger or System.out.println()), and see if it's right. Invalid path bugs happened to me plenty of times, and every time it took a while to find out what's wrong.

String path = uri.getPath();
//breakpoint somewhere below

UPDATE 2:

I've done some research and, apparently, you can't get absolute file path from URI. So I found this solution on StackOverflow.com that gives a super-simple workaround.

Link

Community
  • 1
  • 1
cruelcore1
  • 578
  • 4
  • 22
  • Right. But first I need to know whether image is rotated or not. I can't apply rotation to all images because all other are in right orientation. The problem is with the ones selected from camera folder. – Ammar Jan 04 '15 at 03:55
  • If problem is they were taken in different angels (landscape/portrait), then you can use ExifInterface to determine which angle that is. I've just found the solution in another StackOverflow question. I'll add it in my answer in a few minutes. – cruelcore1 Jan 05 '15 at 04:48
  • I see you already tried that, but you used 2 arguments for the getAttribute method. Try with only 1, ExifInterface.TAG_ORIENTATION, as I posted in the EDIT section of the answer – cruelcore1 Jan 05 '15 at 04:56
  • Tried `exif.getAttribute(ExifInterface.TAG_ORIENTATION)`. Its too giving `0` i.e. `ORIENTATION_UNDEFINED`. – Ammar Jan 05 '15 at 11:36
  • I have posted another update, suggesting to check out what uri.getPath() actually returns. – cruelcore1 Jan 05 '15 at 15:29
  • `uri.getPath()` gives `/document/image:8215` while `uri.toString()` gices `content://com.android.providers.media.documents/document/image%3A8215`. I tried both of them to get `ExifInterface` but result was same in either case. – Ammar Jan 06 '15 at 10:41
  • I've informed myself and getPath doesnt really give absolute path. ive found a solution that you could try. i'm going to update the answer in several minutes – cruelcore1 Jan 06 '15 at 21:26
  • Tried the mentioned link but unfortunately it was also not helpful. But thanks for you input. I've found the solution which is http://stackoverflow.com/a/27821963/1124928 – Ammar Jan 07 '15 at 14:53