Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a camera preview as part of an application I am developing, the preview is in landscape, so I call
camera.setDisplayOrientation(90);
in the
onResume()
method. I give the user an option to retake the image, so when they go back again, it calls the
onResume()
again. I have tested this on the nexus10 and there are no problems, however, when running it on a device running froyo 2.2, it's a htc handset, the app crashes and I get this

07-26 12:19:43.389: E/AndroidRuntime(2245): FATAL EXCEPTION: main
07-26 12:19:43.389: E/AndroidRuntime(2245): java.lang.RuntimeException: set display orientation failed
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.hardware.Camera.setDisplayOrientation(Native Method)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at com.purple.reepfragment.SnapFragment.onResume(SnapFragment.java:114)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.support.v4.app.Fragment.performResume(Fragment.java:1503)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.os.Handler.handleCallback(Handler.java:587)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.os.Looper.loop(Looper.java:144)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at android.app.ActivityThread.main(ActivityThread.java:4937)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at java.lang.reflect.Method.invoke(Method.java:521)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-26 12:19:43.389: E/AndroidRuntime(2245): 	at dalvik.system.NativeStart.main(Native Method)



I really am struggling to figure out what the problem is as setDisplayOrientation is supported in Froyo...
Would anyone have any ideas or have come across this before??
Posted

1 solution

Try with your own method like..
Java
    protected void setDisplayOrientation(Camera camera, int angle){
    Method downPolymorphic;
    try
    {
        downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
        if (downPolymorphic != null)
            downPolymorphic.invoke(camera, new Object[] { angle });
    }
    catch (Exception e1)
    {
    }
}

And instead of using camera.setDisplayOrientation(x) use setDisplayOrientation(camera, x) like,
Java
if (Integer.parseInt(Build.VERSION.SDK) >= 8)
    setDisplayOrientation(mCamera, 90);
else
{
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        p.set("orientation", "portrait");
        p.set("rotation", 90);
    }
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        p.set("orientation", "landscape");
        p.set("rotation", 90);
    }
}
 
Share this answer
 
v2
Comments
GaryDoo 26-Jul-13 8:13am    
p being the camera parameters?
ridoy 26-Jul-13 8:25am    
yes
GaryDoo 26-Jul-13 8:28am    
thank you ridoy!!! I set p to the camera parameters and it works...thank you, although I had noticed that I have implemented this in the correct sequence whereas with the other method I had not, I had called it after my startpreview....anyway, thank you, it's much appreciated
ridoy 26-Jul-13 8:29am    
Glad to help you..:)
GaryDoo 26-Jul-13 8:34am    
can I ask you, do you know how to set autofocus on the camera? I have set it so that if the device is running greater than Gingerbread to implement touch to focus, however, I can't get the device to autofocus if lower than that...would you have any ideas?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900