I am trying to create a Camera that defaults to the back camera (rather then the front) if it available, but I cannot get a preview to display. I have seen many guides on SO (and followed some of them to create what I have now) as well as the Android api, yet I couldnt find an easy way to specify which camera to use. Can someone give me a way to do that, or tell me what is wrong with the code below?
public Camera backCam()
{
Camera cam;
try{
cam=Camera.open(0);
return cam;
}catch(Exception e){}
try{
cam=Camera.open(1);
return cam;
}catch(Exception e){}
cam=Camera.open();
return cam;
}
public void sneakyCamera()
{
try{
Log.i("aaa","init camera. total cams: "+Camera.getNumberOfCameras());
Camera cam=backCam();
SurfaceView view=new SurfaceView(getApplicationContext());
cam.setPreviewDisplay(view.getHolder());
cam.startPreview();
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
File picFile = new File("/mnt/sdcard/","psychPic"+getTime()+".jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(picFile);
fos.write(data);
fos.close();
} catch (IOException e) {
Log.i("aaa","pic tken error: "+e);
}
}
};
//shuttr callback, pic callback raw, pic callback
cam.takePicture(null, null,mCall);
}catch(Exception e)
{
Log.i("aaa","Camera error: "+e);
}
}