0

Code below works great on Wildfire S but no on Motorola RAZR, how it's possible and how to fix it. I saw few posts about that but without answer.

    if (camera==null)
        camera=Camera.open();
    camera_parameters = camera.getParameters();
    flash_mode = camera_parameters.getFlashMode();
    camera_parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(camera_parameters);

    the_button = (ToggleButton) findViewById(R.id.flashlightButton);
    if (the_button.isChecked()){
        camera.startPreview();
        the_button.setKeepScreenOn(true);


public void onToggleClicked(View v) {       
    if (((ToggleButton) v).isChecked()) {
        camera.setParameters(camera_parameters);
        camera.startPreview();
        v.setKeepScreenOn(true);
    } else {
        camera.stopPreview();
        v.setKeepScreenOn(false);
    }
}



    <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.flash" />
Jakub Pomykała
  • 2,082
  • 3
  • 27
  • 58
  • What does the logcat say? – Raghav Sood Mar 23 '13 at 18:49
  • 1
    Nothing interesting, it looks like razr didn't notice I clicked toggle button. Camera is opening and the only error is `03-23 20:01:10.439: E/CameraHal(159): (4005e568) hardware/ti/omap4xxx/camera/CameraHal.cpp:374 setParameters - Setting KEY_MOT_LEDFLASH--100 ` – Jakub Pomykała Mar 23 '13 at 19:02

1 Answers1

1

For whatever reason, the Motorola Razr requires you to use a SurfaceView when accessing anything related to the camera.

    // Where CameraPreview is the class extending SurfaceView
    mPreview = new CameraPreview(this, mCamera);
    preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

The above code must be in the activity you're using to set the Camera.Parameters

The following is a snippet of code from an activity of mine that passes Parameters to the Camera and utilizes the code above to create a SurfaceView

public void setFocusMacro() {

    if (mCamera == null) {
        // Create an instance of Camera
        mCamera = Camera.open();
        mParams = mCamera.getParameters();
        }

    mParams = mCamera.getParameters();
    mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
    mParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    mCamera.setParameters(mParams);
    }
Hollis
  • 39
  • 6