I had the orientation issue when working with ACTION_IMAGE_CAPTURE
activity. I have used the TAG_ORIENTATION
so that I would rotate the picture accordingly. But now we found that on some newer devices this doesn't work. In fact it returns 1 for all orientations.
Here's the list of devices we observed this on;
- Samsung Infuse 4G (2.3.3)
- Samsung Galaxy SII X (2.3.5)
- Sony Xperia Arc (2.3.3)
Interesting thing is that once this image is the gallery it shows up properly and if I select it, the TAG_ORIENTATION
is populated properly. So somehow the OS
fills this information properly but not on ActivityResult
.
What's the most reliable way to figure the orientation? Someone on another question suggested comparing height and width but when getting these, they are properly switched based on orientation (another mystery)
EDIT: It seems that this could be connected to another bug where the OS duplicates the image taken in the gallery (it's only supposed to save the image in the URL specified by us), the thing is this image in gallery has the ORIENTATION
information while the one in the specified location doesn't.
This is the bug; http://code.google.com/p/android/issues/detail?id=19268
EDIT-2: I've filed a new bug with Android. I'm pretty sure this is an OS bug related the aforementioned bug. http://code.google.com/p/android/issues/detail?id=22822
Ok guys, it seems like this bug for android won't be fixed for a while. Although I found a way to implement the ExifInformation so that both devices (ones with proper Exif tag, and also improper exif tags work together)..
So the issue is on some (newer) devices, there's a bug that makes the picture taken saved in your app folder without proper exif tags while a properly rotated image is saved in the android default folder (even though it shouldn't be)..
Now what I do is, i record the time when I'm starting the camera app from my app. THen on activity result, I query the Media Provider to see if any pictures were saved after this timestamp I saved. That means that, most likely OS saved the properly rotated picture in the default folder and of course put a entry in the media store and we can use the rotation information from this row. Now to make sure we are looking at the right image, i compare the size of this file to the one I have access to (saved in my own app folder);
Now if the rotation at this point is still -1, then that means this is one of the phones with proper rotation information. At this point, we can use the regular exif orientation on the file that's returned to our onActivityResult
You can easily find out how to find exif orientations like the answer in this question Camera orientation issue in Android
Also note that ExifInterface is only supported after Api level 5.. So if you want to support phones before 2.0, then you can use this handy library I found for java courtesy of Drew Noakes; http://www.drewnoakes.com/code/exif/
Good luck with your image rotating!
EDIT: Because it was asked, the intent I've used and how i started was like this
you can go this way too:
My solution for this. Tested on LG G2 mobile. I noticed that when I use camera and take new picture everything works fine. ExifInterface returns right orientation. So it must be something in the path because my path was null in this line of code:
but when i used absolute path my app crash. But the solution is in this method below, because it depends on your sdk version. One more note to mention I used absolute path only for selecting gallery picture because if i used it for Camera my app crashed. Im new in programing and just lost 2 days to solve this. Hope it will help someone.
So I get my ExifInterface in onActivityResult method
and my show image method look like this
What I've learned recently is that if you resize the image, it usually loses its EXIF information. So you want to give the new file the old EXIF information.
Source.
Indeed a problematic bug! I'm not sure I like the suggested workaround, so here's another :)
The key is to use
EXTRA_OUTPUT
and query it when the image has been captured! Obviously, this only works if you allow yourself to specify the filename.