I am creating a camera application, but I have problems with startPreview , it sends me:
java.lang.RuntimeException: startPreview failed
here is my camera. Activity:
public class CameraActivity extends Activity { private Camera mCamera; private CameraPreview mPreview; private Target_Frame targetFrame; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.camera_layout); mCamera=getCameraInstance(); mPreview=new CameraPreview(this, mCamera); FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview); preview.addView(mPreview); } /** Check if this device has a camera only if not specified in the manifest */ public boolean checkCameraHardware(Context context) { if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ // this device has a camera return true; } else { // no camera on this device return false; } } /** A safe way to get an instance of the Camera object. */ 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 } /**Check if the device has flash*/ public boolean checkFlash(Context context){ if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){ //the device has flash return true; }else{ //no flash return false; } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); releaseCamera(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); releaseCamera(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); //Test if i have to put all this code like in onCreate if(mCamera!=null){ return; } mCamera=getCameraInstance(); if(mPreview!=null){ return; } mPreview=new CameraPreview(this, mCamera); FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview); preview.addView(mPreview); } private void releaseCamera(){ if (mCamera != null){ mCamera.release(); // release the camera for other applications mCamera = null; } }}
And here is my SurfaceView code:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private static final String TAG = "CameraPreview"; private SurfaceHolder mHolder; private Camera mCamera; public CameraPreview(Context context, Camera camera) { super(context); mCamera = camera;
And here is my error log:
12-01 13:17:01.135: E/AndroidRuntime(1161): FATAL EXCEPTION: main 12-01 13:17:01.135: E/AndroidRuntime(1161): java.lang.RuntimeException: startPreview 12-01 13:17:01.135: E/AndroidRuntime(1161): at com.example.prueba.CameraPreview.surfaceCreated(CameraPreview.java:36)
user1805792
source share