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 <vvnevercha...@gmail.com> 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<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); > 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<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"); > } > } > } > } > > } > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > 2012/2/8 Matt Clark <mrclark32...@gmail.com> > > > > > > > > > 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<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)) { > > ... > > read more » -- 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