I developed a camera app in android. It captures an image using a surface view. Below is the code I used to open the camera
try { // attempt to get a Front Camera instance c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); } catch (Exception e) { // TODO Auto-generated catch block System.out .println("fail to connect to Front Camera"); } if (c == null) { try { // attempt to get a Back Camera instance c = Camera.open(1); } catch (Exception e) { // TODO: handle exception System.out .println("fail to connect to Camera with id = 1"); } } if (c == null) { try { // attempt to get a Back Camera instance c = Camera.open(0); } catch (Exception e) { // TODO: handle exception System.out .println("fail to connect to Camera with id = 0"); } } if (c == null) { try { // attempt to get a Back Camera instance c = Camera.open(); } catch (Exception e) { // TODO: handle exception System.out .println("fail to connect to Back Camera"); return c; }
Where c is the camera object.
It works fine in other phones except the Nexus 7 tablet. In Nexus 7, the code throws an exception in all cases except the last one, i.e. c = Camera.open(); but still the object c is null .
Here is the stack trace
11-22 12:36:57.559 W/System.err(7621): java.lang.NullPointerException 11-22 12:36:57.559 W/System.err(7621): at com.MyPackage.OpenCamera.getFrontCameraInstance(OpenCamera.java:238) 11-22 12:36:57.559 W/System.err(7621): at com.MyPackage.OpenCamera.onCreate(OpenCamera.java:123) 11-22 12:36:57.559 W/System.err(7621): at android.app.Activity.performCreate(Activity.java:5104) 11-22 12:36:57.559 W/System.err(7621): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 11-22 12:36:57.559 W/System.err(7621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 11-22 12:36:57.559 W/System.err(7621): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 11-22 12:36:57.559 W/System.err(7621): at android.app.ActivityThread.access$600(ActivityThread.java:141) 11-22 12:36:57.559 W/System.err(7621): at android.app.ActivityThread $H.handleMessage(ActivityThread.java:1234) 11-22 12:36:57.559 W/System.err(7621): at android.os.Handler.dispatchMessage(Handler.java:99) 11-22 12:36:57.559 W/System.err(7621): at android.os.Looper.loop (Looper.java:137) 11-22 12:36:57.559 W/System.err(7621): at android.app.ActivityThread.main (ActivityThread.java:5039) 11-22 12:36:57.559 W/System.err(7621): at java.lang.reflect.Method.invokeNative(Native Method) 11-22 12:36:57.559 W/System.err(7621): at java.lang.reflect.Method.invoke (Method.java:511) 11-22 12:36:57.559 W/System.err(7621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:793) 11-22 12:36:57.559 W/System.err(7621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 11-22 12:36:57.559 W/System.err(7621): at dalvik.system.NativeStart.main (Native Method) 11-22 12:36:57.559 I/System.out(7621): Error in setting Parameter 11-22 12:36:57.609 I/ActivityManager(480): Displayed 11-22 12:36:57.679 W/System.err(7621): at android.location.Geocoder.getFromLocation(Geocoder.java:136) 11-22 12:36:57.679 W/System.err(7621): at com.MyPackage.OpenCamera. $MyTimmer$1.run(OpenCamera.java:336) 11-22 12:36:57.679 W/System.err(7621): java.lang.NullPointerException 11-22 12:36:57.679 W/System.err(7621): at com.MyPackage.OpenCamera. $MyTimmer$1.run(OpenCamera.java:344)
Therefore, I cannot use it to capture an image. Any solution ???
Thanks...
source share