5

I have 2 androids here.

In both of them, when I turn on my app, the camera shows up horribly wrong (turned 90 degrees sideways and stretched usually...)

In one of the phones, there are a keyboard, and when I open the keyboard the app work correctly... Since the keyboard forces a orientation, I figured that the issue is that the phones expect always the same orientation but the SDK disagrees.

How I then inform the camera what orientation it is supposed to use all the time?

speeder
  • 6,197
  • 5
  • 34
  • 51

1 Answers1

20

If your application runs on v2.2 or above you can rotate camera orientation to portrait using camera.setDisplayOrientation(90).

Prior to v2.2 the camera will only display in landscape orientation, so you're forced to set the activity to landscape orientation.

To support devices prior to v2.2 (API level 8) and after, one solution is to default the activity orientation to landscape in AndroidManifest.xml. Then at runtime check the API level and if froyo or above, change the activity orientation to portrait and rotate the camera display.

//in activity onCreate method
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//After opening camera - call via reflection
Method rotateMethod = android.hardware.Camera.class.getMethod("setDisplayOrientation", int.class);
rotateMethod.invoke(mCamera, 90);

This is the most straightforward solution and hopefully as new devices come out v2.1 and below will drop off the radar.

Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26
  • Oh, I see the issue now (the keyboard forces the entire app into landscape, making the camera work correctly). – speeder Mar 31 '11 at 18:40
  • Yeah it's a pain for devices below v2.2. I've built a camera application that detects if the device is v2.2 and sets the activity to portrait as well as rotating the camera display using setDisplayOrientation. Devices v2.1 and below get the landscape look and feel. – Ryan Reeves Mar 31 '11 at 18:41
  • the problem I have with setRequestedOrientation is, that I can't request the orientation of the phone (I always get PORTRAIT from the API), and therefore I don't know whether to rotate the images taken by the camera or not :/ and hints on that? – stoefln Dec 17 '11 at 10:31
  • @Ryan Reeves this fix works on some handsets and does not work on some other handsets. I am using Android 2.2 SDK and it has been a while I am not able find answer, tried with google groups code fix also but not working. Any more pointers or answers would be helpful. thanks. – Mukunda Dec 30 '11 at 07:20