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<String> 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<String> 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 <mrclark32...@gmail.com> 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: > > <uses-permission android:name="android.permission.FLASHLIGHT" /> > <uses-parmission android:name="android.permission.CAMERA" /> > <uses-feature android:name="android.hardware.camera" /> > <uses-feature android:name="android.hardware.camera.flash" /> > > 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