[android-developers] Re: Fail to connect to camera service <- Camera.open();
I tried both codes on multiple phones each, and it still fails to open the camera. Every time, even after a reboot. On Feb 9, 12:31 am, v bobbywink wrote: > i change a little codes then test it in my phone. It works very well. > Test it in your phone and if it don't work i have no idea > Maybe u should test it in another phone. > > > > public class LightTest extends Activity{ > String TAG = "tinyClark"; > boolean mIsFlashOpen; > Camera mCamera; > > // Camera.Parameters CameraInfo; > > Button btn1; > //Boolean lightToggle; > > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > btn1 = new Button(this); > btn1.setOnClickListener(new View.OnClickListener() { > > @Override > public void onClick(View v) { > if(mIsFlashOpen == false){ > turnLightOn(); > }else{ > turnLightOff(); > } > } > }); > setContentView(btn1); > > } > > @Override > public void onStop() { > turnLightOff(); > if (null != mCamera) { > mCamera.release(); > mCamera = null; > } > super.onStop(); > } > > private void turnLightOn() { > > if (false == mIsFlashOpen) { > if (null == mCamera) { > try { > Log.e(TAG, "open"); > mCamera = > Camera.open(CameraInfo.CAMERA_FACING_BACK); > } catch (RuntimeException e) { > try { > Thread.sleep(500); > Log.e(TAG, "second open"); > mCamera = > Camera.open(CameraInfo.CAMERA_FACING_BACK); > } catch (Exception sube) { > Log.e(TAG, "fail to open > camera"); > sube.printStackTrace(); > mCamera = null; > } > } > } > if (null != mCamera) { > Parameters parameters = mCamera.getParameters(); > if (parameters == null) { > return; > } > List flashModes = > parameters.getSupportedFlashModes(); > // Check if camera flash exists > if (flashModes == null) { > return; > } > String flashMode = parameters.getFlashMode(); > if > (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) { > // Turn on the flash > if > (flashModes.contains(Parameters.FLASH_MODE_TORCH)) { > > parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); > mCamera.startPreview(); > > mCamera.setParameters(parameters); > mIsFlashOpen = true; > // mRightBtn.setEnabled(true); > } else { > Log.e(TAG, "FLASH_MODE_TORCH > not supported"); > } > } > } > } > } > > private void turnLightOff() { > if (mIsFlashOpen) { > mIsFlashOpen = false; > if (mCamera == null) { > return; > } > Parameters parameters = mCamera.getParameters(); > if (parameters == null) { > return; > } > List flashModes = > parameters.getSupportedFlashModes(); > String flashMode = parameters.getFlashMode(); > // Check if camera flash exists > if (flashModes == null) { > return; > } > if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) { > // Turn off the flash > if > (flashModes.contains(Parameters.FLASH_MODE_OFF)) { > > parameters.setFlashMode(Parameters.FLASH_MODE_OFF); > mCamera.setParameters(parameters); >
[android-developers] Re: Fail to connect to camera service <- Camera.open();
package tinyClark.util.flashlite; import java.util.List; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.CameraInfo; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class Master extends Activity { String TAG = "tinyClark"; boolean mIsFlashOpen; Camera mCamera; // Camera.Parameters CameraInfo; Button btn1; Boolean lightToggle; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(lightToggle == false){ turnLightOn(); lightToggle = true; }else{ turnLightOff(); lightToggle = false; } } }); } private void turnLightOn() { if (false == mIsFlashOpen) { if (null == mCamera) { try { Log.e(TAG, "open"); mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK); } catch (RuntimeException e) { try { Thread.sleep(500); Log.e(TAG, "second open"); mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK); } catch (Exception sube) { Log.e(TAG, "fail to open camera"); sube.printStackTrace(); mCamera = null; } } } if (null != mCamera) { Parameters parameters = mCamera.getParameters(); if (parameters == null) { return; } List flashModes = parameters.getSupportedFlashModes(); // Check if camera flash exists if (flashModes == null) { return; } String flashMode = parameters.getFlashMode(); if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) { // Turn on the flash if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) { parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); mCamera.startPreview(); mCamera.setParameters(parameters); // mRightBtn.setEnabled(true); } else { Log.e(TAG, "FLASH_MODE_TORCH not supported"); } } } } } private void turnLightOff() { if (mIsFlashOpen) { mIsFlashOpen = false; if (mCamera == null) { return; } Parameters parameters = mCamera.getParameters(); if (parameters == null) { return; } List flashModes = parameters.getSupportedFlashModes(); String flashMode = parameters.getFlashMode(); // Check if camera flash exists if (flashModes == null) { return; } if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) { // Turn off the flash if (flashModes.contains(Parameters.FLASH_MODE_OFF)) { parameters.setFlashMode(Parameters.FLASH_MODE_OFF); mCamera.se
Re: [android-developers] Re: Fail to connect to camera service <- Camera.open();
The camera can only be used by one application at a time. You must release it when you are done with it, so that you may use it again. The other applications can still use the camera as your app force closes, which frees up the camera. Thanks 2012/2/8 v bobbywink > So strange, can u post all code in this activity? > > > 2012/2/8 Matt Clark > >> Even using your code, in LogCat i get: >> open >> second open >> fail to open camera >> This happens even after rebooting the phone, however all other >> applications that use the camera still work. >> >> On Feb 7, 2:45 am, v bobbywink wrote: >> > well, i think camera has crashed and in the condition u can't get >> > camera service. U must reboot phone. >> > when using hardware , u should be careful. u can open Camera in >> > onStart() and release Camera in onStop(). Can't open camera many times >> > in activity, when task finish u must release it . >> > sample code >> > private void turnLightOn() { >> > if (false == mIsFlashOpen) { >> > if (null == mCamera) { >> > try { >> > Log.e(TAG, "open"); >> > mCamera =Camera.open(CameraInfo.CAMERA_FACING_BACK); >> > } catch (RuntimeException e) { >> > try { >> > Thread.sleep(500); >> > Log.e(TAG, "second open"); >> > mCamera >> =Camera.open(CameraInfo.CAMERA_FACING_BACK); >> > } catch (Exception sube) { >> > Log.e(TAG, "fail to open camera"); >> > sube.printStackTrace(); >> > mCamera = null; >> > } >> > } >> > } >> > if (null != mCamera) { >> > Parameters parameters = mCamera.getParameters(); >> > if (parameters == null) { >> > return; >> > } >> > List flashModes = >> > parameters.getSupportedFlashModes(); >> > // Check if camera flash exists >> > if (flashModes == null) { >> > return; >> > } >> > String flashMode = parameters.getFlashMode(); >> > if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) { >> > // Turn on the flash >> > if >> > (flashModes.contains(Parameters.FLASH_MODE_TORCH)) { >> > >> > parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); >> > mCamera.startPreview(); >> > mCamera.setParameters(parameters); >> > mRightBtn.setEnabled(true); >> > } else { >> > Log.e(TAG, "FLASH_MODE_TORCH not supported"); >> > } >> > } >> > } >> > } >> > } >> > >> > private void turnLightOff() { >> > if (mIsFlashOpen) { >> > mIsFlashOpen = false; >> > if (mCamera == null) { >> > return; >> > } >> > Parameters parameters = mCamera.getParameters(); >> > if (parameters == null) { >> > return; >> > } >> > List flashModes = >> > parameters.getSupportedFlashModes(); >> > String flashMode = parameters.getFlashMode(); >> > // Check if camera flash exists >> > if (flashModes == null) { >> > return; >> > } >> > if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) { >> > // Turn off the flash >> > if (flashModes.contains(Parameters.FLASH_MODE_OFF)) { >> > >> > parameters.setFlashMode(Parameters.FLASH_MODE_OFF); >> > mCamera.setParameters(parameters); >> > mCamera.stopPreview(); >> > } else { >> > Log.e(TAG, "FLASH_MODE_OFF not supported"); >> > } >> > } >> > } >> > } >> > >> > On 2月7日, 下午1时14分, Matt Clark wrote: >> > >> > >> > >> > >> > >> > >> > >> > > Even as hundreds of other have made them, i am trying my hand at a >> > > flashlight app. I have the basic GUI and widget working and toggling, >> > > and i just attempted to implement the actual flash from the phone. >> > > I have found many places that this is most easily done with this >> > > snippet of code: >> > >> > > Camera mCamera =Camera.open(); >> > > Parameters params = mCamera.getParameters(); >> > > params.setFlashMode( Parameters.FLASH_MODE_TORCH ); >> > > mCamera.setParameters( params ); >> > >> > > I have tried this code, and every time i run the app it crashes, >> > > giving me an output in LogCat of the following: >> > >> > > 02-07 00:04:49.130: E/AndroidRuntime(4861): Caused by: >> > > java.lang.RuntimeException: Fail to connect to camera service >> > >> > > Every other app that uses the camera or fla
Re: [android-developers] Re: Fail to connect to camera service <- Camera.open();
So strange, can u post all code in this activity? 2012/2/8 Matt Clark > Even using your code, in LogCat i get: > open > second open > fail to open camera > This happens even after rebooting the phone, however all other > applications that use the camera still work. > > On Feb 7, 2:45 am, v bobbywink wrote: > > well, i think camera has crashed and in the condition u can't get > > camera service. U must reboot phone. > > when using hardware , u should be careful. u can open Camera in > > onStart() and release Camera in onStop(). Can't open camera many times > > in activity, when task finish u must release it . > > sample code > > private void turnLightOn() { > > if (false == mIsFlashOpen) { > > if (null == mCamera) { > > try { > > Log.e(TAG, "open"); > > mCamera =Camera.open(CameraInfo.CAMERA_FACING_BACK); > > } catch (RuntimeException e) { > > try { > > Thread.sleep(500); > > Log.e(TAG, "second open"); > > mCamera > =Camera.open(CameraInfo.CAMERA_FACING_BACK); > > } catch (Exception sube) { > > Log.e(TAG, "fail to open camera"); > > sube.printStackTrace(); > > mCamera = null; > > } > > } > > } > > if (null != mCamera) { > > Parameters parameters = mCamera.getParameters(); > > if (parameters == null) { > > return; > > } > > List flashModes = > > parameters.getSupportedFlashModes(); > > // Check if camera flash exists > > if (flashModes == null) { > > return; > > } > > String flashMode = parameters.getFlashMode(); > > if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) { > > // Turn on the flash > > if > > (flashModes.contains(Parameters.FLASH_MODE_TORCH)) { > > > > parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); > > mCamera.startPreview(); > > mCamera.setParameters(parameters); > > mRightBtn.setEnabled(true); > > } else { > > Log.e(TAG, "FLASH_MODE_TORCH not supported"); > > } > > } > > } > > } > > } > > > > private void turnLightOff() { > > if (mIsFlashOpen) { > > mIsFlashOpen = false; > > if (mCamera == null) { > > return; > > } > > Parameters parameters = mCamera.getParameters(); > > if (parameters == null) { > > return; > > } > > List flashModes = > > parameters.getSupportedFlashModes(); > > String flashMode = parameters.getFlashMode(); > > // Check if camera flash exists > > if (flashModes == null) { > > return; > > } > > if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) { > > // Turn off the flash > > if (flashModes.contains(Parameters.FLASH_MODE_OFF)) { > > > > parameters.setFlashMode(Parameters.FLASH_MODE_OFF); > > mCamera.setParameters(parameters); > > mCamera.stopPreview(); > > } else { > > Log.e(TAG, "FLASH_MODE_OFF not supported"); > > } > > } > > } > > } > > > > On 2月7日, 下午1时14分, Matt Clark wrote: > > > > > > > > > > > > > > > > > Even as hundreds of other have made them, i am trying my hand at a > > > flashlight app. I have the basic GUI and widget working and toggling, > > > and i just attempted to implement the actual flash from the phone. > > > I have found many places that this is most easily done with this > > > snippet of code: > > > > > Camera mCamera =Camera.open(); > > > Parameters params = mCamera.getParameters(); > > > params.setFlashMode( Parameters.FLASH_MODE_TORCH ); > > > mCamera.setParameters( params ); > > > > > I have tried this code, and every time i run the app it crashes, > > > giving me an output in LogCat of the following: > > > > > 02-07 00:04:49.130: E/AndroidRuntime(4861): Caused by: > > > java.lang.RuntimeException: Fail to connect to camera service > > > > > Every other app that uses the camera or flash works fine, and i have > > > no idea what is causing it to fail. > > > > > In my android manifest i have: > > > > > > > > > > > > > > > > > > > Any and all help is greatly appreciated as I am completely stuck after > > > sitting at this for the last several hours... > > > Thanks > > > ~Matt > > -- > You received this message because you are subscribed to the Google > Groups "Android De
[android-developers] Re: Fail to connect to camera service <- Camera.open();
Even using your code, in LogCat i get: open second open fail to open camera This happens even after rebooting the phone, however all other applications that use the camera still work. On Feb 7, 2:45 am, v bobbywink wrote: > well, i think camera has crashed and in the condition u can't get > camera service. U must reboot phone. > when using hardware , u should be careful. u can open Camera in > onStart() and release Camera in onStop(). Can't open camera many times > in activity, when task finish u must release it . > sample code > private void turnLightOn() { > if (false == mIsFlashOpen) { > if (null == mCamera) { > try { > Log.e(TAG, "open"); > mCamera =Camera.open(CameraInfo.CAMERA_FACING_BACK); > } catch (RuntimeException e) { > try { > Thread.sleep(500); > Log.e(TAG, "second open"); > mCamera =Camera.open(CameraInfo.CAMERA_FACING_BACK); > } catch (Exception sube) { > Log.e(TAG, "fail to open camera"); > sube.printStackTrace(); > mCamera = null; > } > } > } > if (null != mCamera) { > Parameters parameters = mCamera.getParameters(); > if (parameters == null) { > return; > } > List flashModes = > parameters.getSupportedFlashModes(); > // Check if camera flash exists > if (flashModes == null) { > return; > } > String flashMode = parameters.getFlashMode(); > if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) { > // Turn on the flash > if > (flashModes.contains(Parameters.FLASH_MODE_TORCH)) { > > parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); > mCamera.startPreview(); > mCamera.setParameters(parameters); > mRightBtn.setEnabled(true); > } else { > Log.e(TAG, "FLASH_MODE_TORCH not supported"); > } > } > } > } > } > > private void turnLightOff() { > if (mIsFlashOpen) { > mIsFlashOpen = false; > if (mCamera == null) { > return; > } > Parameters parameters = mCamera.getParameters(); > if (parameters == null) { > return; > } > List flashModes = > parameters.getSupportedFlashModes(); > String flashMode = parameters.getFlashMode(); > // Check if camera flash exists > if (flashModes == null) { > return; > } > if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) { > // Turn off the flash > if (flashModes.contains(Parameters.FLASH_MODE_OFF)) { > > parameters.setFlashMode(Parameters.FLASH_MODE_OFF); > mCamera.setParameters(parameters); > mCamera.stopPreview(); > } else { > Log.e(TAG, "FLASH_MODE_OFF not supported"); > } > } > } > } > > On 2月7日, 下午1时14分, Matt Clark wrote: > > > > > > > > > Even as hundreds of other have made them, i am trying my hand at a > > flashlight app. I have the basic GUI and widget working and toggling, > > and i just attempted to implement the actual flash from the phone. > > I have found many places that this is most easily done with this > > snippet of code: > > > Camera mCamera =Camera.open(); > > Parameters params = mCamera.getParameters(); > > params.setFlashMode( Parameters.FLASH_MODE_TORCH ); > > mCamera.setParameters( params ); > > > I have tried this code, and every time i run the app it crashes, > > giving me an output in LogCat of the following: > > > 02-07 00:04:49.130: E/AndroidRuntime(4861): Caused by: > > java.lang.RuntimeException: Fail to connect to camera service > > > Every other app that uses the camera or flash works fine, and i have > > no idea what is causing it to fail. > > > In my android manifest i have: > > > > > > > > > > > > Any and all help is greatly appreciated as I am completely stuck after > > sitting at this for the last several hours... > > Thanks > > ~Matt -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Fail to connect to camera service <- Camera.open();
well, i think camera has crashed and in the condition u can't get camera service. U must reboot phone. when using hardware , u should be careful. u can open Camera in onStart() and release Camera in onStop(). Can't open camera many times in activity, when task finish u must release it . sample code private void turnLightOn() { if (false == mIsFlashOpen) { if (null == mCamera) { try { Log.e(TAG, "open"); mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK); } catch (RuntimeException e) { try { Thread.sleep(500); Log.e(TAG, "second open"); mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK); } catch (Exception sube) { Log.e(TAG, "fail to open camera"); sube.printStackTrace(); mCamera = null; } } } if (null != mCamera) { Parameters parameters = mCamera.getParameters(); if (parameters == null) { return; } List flashModes = parameters.getSupportedFlashModes(); // Check if camera flash exists if (flashModes == null) { return; } String flashMode = parameters.getFlashMode(); if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) { // Turn on the flash if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) { parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); mCamera.startPreview(); mCamera.setParameters(parameters); mRightBtn.setEnabled(true); } else { Log.e(TAG, "FLASH_MODE_TORCH not supported"); } } } } } private void turnLightOff() { if (mIsFlashOpen) { mIsFlashOpen = false; if (mCamera == null) { return; } Parameters parameters = mCamera.getParameters(); if (parameters == null) { return; } List flashModes = parameters.getSupportedFlashModes(); String flashMode = parameters.getFlashMode(); // Check if camera flash exists if (flashModes == null) { return; } if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) { // Turn off the flash if (flashModes.contains(Parameters.FLASH_MODE_OFF)) { parameters.setFlashMode(Parameters.FLASH_MODE_OFF); mCamera.setParameters(parameters); mCamera.stopPreview(); } else { Log.e(TAG, "FLASH_MODE_OFF not supported"); } } } } On 2月7日, 下午1时14分, Matt Clark wrote: > Even as hundreds of other have made them, i am trying my hand at a > flashlight app. I have the basic GUI and widget working and toggling, > and i just attempted to implement the actual flash from the phone. > I have found many places that this is most easily done with this > snippet of code: > > Camera mCamera = Camera.open(); > Parameters params = mCamera.getParameters(); > params.setFlashMode( Parameters.FLASH_MODE_TORCH ); > mCamera.setParameters( params ); > > I have tried this code, and every time i run the app it crashes, > giving me an output in LogCat of the following: > > 02-07 00:04:49.130: E/AndroidRuntime(4861): Caused by: > java.lang.RuntimeException: Fail to connect to camera service > > Every other app that uses the camera or flash works fine, and i have > no idea what is causing it to fail. > > In my android manifest i have: > > > > > > > Any and all help is greatly appreciated as I am completely stuck after > sitting at this for the last several hours... > Thanks > ~Matt -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Fail to connect to camera service <- Camera.open();
Anything? On Feb 7, 12:14 am, Matt Clark wrote: > Even as hundreds of other have made them, i am trying my hand at a > flashlight app. I have the basic GUI and widget working and toggling, > and i just attempted to implement the actual flash from the phone. > I have found many places that this is most easily done with this > snippet of code: > > Camera mCamera = Camera.open(); > Parameters params = mCamera.getParameters(); > params.setFlashMode( Parameters.FLASH_MODE_TORCH ); > mCamera.setParameters( params ); > > I have tried this code, and every time i run the app it crashes, > giving me an output in LogCat of the following: > > 02-07 00:04:49.130: E/AndroidRuntime(4861): Caused by: > java.lang.RuntimeException: Fail to connect to camera service > > Every other app that uses the camera or flash works fine, and i have > no idea what is causing it to fail. > > In my android manifest i have: > > > > > > > Any and all help is greatly appreciated as I am completely stuck after > sitting at this for the last several hours... > Thanks > ~Matt -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en