I've created a camera app for android with the android camera code tutorial http://developer.android.com/guide/topics/media/camera.html#custom-camera. It works for the XperiaZ - LgoptimusBlack- GalaxyNexus4 and some other devices. But with the Nexus-7 android 4.2.2 it gave's me an surfaceview error , it cannot create the camera preview on the surface. I've tried some other samples me but without results. here is the logcat :
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.test.mycamera.CameraPreview.surfaceCreated(CameraPreview.java:33)
at android.view.SurfaceView.updateWindow(SurfaceView.java:569)
at android.view.SurfaceView.access$000(SurfaceView.java:86)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:174)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1842)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
The Nexus 7 has only a front camera.
The Google code fails to deal with this fact. It uses:
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
But then:
mCamera = getCameraInstance();
And no check whether that works.
The problem is that Camera.open()
will return null
if there is no rear camera:
Creates a new Camera object to access the first back-facing camera on the device. If the device does not have a back-facing camera, this returns null.
Ironically, Camera.open(0);
should work, but blindly selecting the camera with ID 0 is definitely not what you want in a real product. Also, note the following which is stated for open(int cameraId)
but not for open()
:
Caution: On some devices, this method may take a long time to complete. It is best to call this method from a worker thread (possibly using AsyncTask) to avoid blocking the main application UI thread.
Then again, you don't necessarily want to use Google code fragments in real products, because of their, hmm, maturity level. As we just saw.
Good luck.