Get android device orientation even when autoRotation is turned off - android

I have an android app in which the screen orientation is set to portrait in manifest file.
It uses a custom camera in b/w , to take a photo and I need to save the photo.
Before I save the image I have to rotate the image depending on how the user is holding the camera, so that the image gets saved in the right orientation.
So, is there any way by which I can get the device orientation even when my app is running in portrait mode and android "auto-rotation" feature is ON.
How can I achieve this?

You should do the rotation depending on the orientation information within the image, not depending on the device orientation.
Here is some more information on how to determine the rotation.
Since this does not help, have you tried hackpod's answer here:
getResources().getConfiguration().orientation

To get current device orientation try this:
getWindowManager().getDefaultDisplay().getRotation();

Simply check, the height and width of the image. If the height is less than your width, it means the user was holding the phone horizontally, so you have to rotate it.

I am currently using android orientation sensor to solve this problem and its working perfectly. Till someone gives a better working method , am accepting this as answer for other who have the same question.

Related

How to force change layouts in Android

I have my app's orientation forced via
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
but when I want to change layout to landscape one (when device is rotated - i get the info via OrientationEventListener) it just doesnt render it as landscape. Is there any way how to force it? Just like imagine holding your device in portait mode but the layout is landscape. but not achieved via
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
android:screenOrientation="landscape" in manifest
I want to have it forced to portait because I'm building a camera app and it messes up the camera everytime and I want to have it like the default camera has it - no change, just the button orientation. If that makes sense.
A lot of camera apps achieve this by rotating Views and it's a good way to do it because you don't have to deal with Activity lifecycle as well as camera lifecycle. They have a locked orientation but they use accelerometer to get device rotation and then rotate Views accordingly.
Camera API gives you ability to set camera rotation as well as picture orientation. Picture orientation should be set to zero because Camera API is unreliable with this.
Use accelerometer to determine real device rotation and set EXIF rotation of the picture to a correct one.

Android: correct way to get screen dimensions for target orientation?

My application is bitmap intensive, with pixel-exact layout (it's a sort of game, actually, and it's pretty hard to avoid this pixel-based coordinates).
What I wanted to do is to perform some layout calculations and bitmap pre-scaling in my onCrete - I use well known API - getWindowManager().getDefaultDisplay().getSize() - to retrieve the screen size and do my calculations.
However, I've just hit an unexpected problem. My activity is configured as landscape only, but if I start my application on emulator and onCreate() is called while the emulator is locked, the screen size returned by getSize() indicates portrait orientation. Once I unlock the screen, onCreate() is called again, this time correctly in line with expected landscape mode dimensions.
I'm not sure how to handle this situation. I see the following options:
for each onCreate() call perform full layout calculation and resource scaling again. This is the logically correct solution, but I don't want to the same work twice, just to throw away the first result.
if onCreate() is called for portrait mode, just do nothing, and set black background (I can see there's a silly rotate animation when I unlock the screen, so this would become pretty much a fade-in animation)
Actually I'd prefer second option, but I'm slightly afraid of any side-effects. Anyone faced this problem?
Update (2012-07-08):
I've probably assigned a slightly misleading title to this question (sorry!), as the problem is not in retrieving the dimensions itself, nor calculating the layout. It's more about the activity being first created in portrait mode, and then recreated in landscape mode again, despite being declared as landscape-only. I initially expected (reasonably, huh?) the activity to be created in landscape orientation only.
I eventually decided to fill the activity with black color when it's created in portrait mode, no side effects observed. On Android 4.0 I can see actual rotation animation when I unlock the screen - a bit strange, but well, I guess it is supposed to inform the user that she should rotate the phone. Given that in portrait mode I just fill the screen with black color, this animation looks sort of like a fade-in and rotation combined - perfectly acceptable.
Use that
DisplayMetrics dm=new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
Using this(Look code at down) only gives you screen size and if your views has static size they will be seen in different size on every different screen.
Display screen=getWindowManager().getDefaultDisplay().getSize();
How to use:
every screen has diffrent density. So use:
float density=dm.density;
with this density, you can set your views size like that:
(YOUR_ITEM_SIZE)*density;
also look here for additional information:
http://developer.android.com/reference/android/util/DisplayMetrics.html
if the emulator is locked , can't you assume that the user can't run anything anyway , so the app doesn't need to handle this end case ?
anyway , as bmavus wrote , use getMetrics for getting the screen size . also , if you need to change the screen orientation of the app , you can do so either in the manifest or in code.
for games , i would advice using opengl solutions , and if you don't have much time digging for it , you can use third party engines that can help you , such as andengine and libgdx.

Check a screen orientation when it's fixed by manifest file

I have an camera activity in my app, where user can make a photo. For some reasons it's fixed to landscape mode(by the screenOrientation in manifest). All works perfectly but I need to know - what screen mode on picture? User can rotate device and try to make a photo in portrait mode.
I've tried to use getRotation method but it returnes only 0 degrees, cause i'm set to portrait screenOrientation.
I want to ask - is there some ways to solve this problem?
Thanks for any help!
Use the orientation sensor, ain't as difficult as you might think.
http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/
Android phone orientation overview including compass

Android activity rotation locked in manifest, but need to grab the orientation event

I am designing a application that uses the camera. I want it locked it landscape mode and just have the pictures(for example, a handle to change between camera and video) rotate. I don't want to launch onCreate() again. This is so the camera/video logo does not appear sideways on the screen.
I have put this in the manifest:
android:screenOrientation="landscape"
This keeps the screen from turning. However, I can't seem to figure out how I get the event that tells me when turn the images. I have seen camera apps do this.
Thanks!
If you've locked your activity using manifest, the only workable solution to track orientation I saw was using OrientationEventListener.
You'll need to convert degrees to correct orientation yourself. But that is relatively easy task.

Problem detecting orientation of phone

In my onCreate method, I'd like to detect the orientation and set an appropriate background image.
I can get the orientation like so:
Display dis = getWindowManager().getDefaultDisplay();
int orientation = dis.getOrientation();
I've tested this on both the Galaxy S and the Moment.
When I am in portrait mode, this returns a value of 0. When I am in landscape mode, this returns a value of 1.
However, the define Configuration.ORIENTATION_LANDSCAPE has a value of 2 and the define Configuration.ORIENTATION_PORTRAIT has a value of 1.
So, when I turn the phone to landscape mode, it gives me Configuration.ORIENTATION_PORTRAIT. And when I turn the phone to portrait mode, it gives me Configuration.ORIENTATION_UNDEFINED.
What is going on???
I'm using API level 7
One thing you could do is just put the landscape drawable into a /drawable-land/ folder, and Android will pull it automatically depending on the orientation. Rather than relying on that, though, you would be better off to make a landscape version of the layout under /layout-land/ that has the alternate version as its background.
Look at the answers in Check orientation on Android phone

Categories

Resources