Jakub
Jakub

Reputation: 2141

Android Flashlight on Motorola RAZR

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" />

Upvotes: 0

Views: 568

Answers (1)

Hollis
Hollis

Reputation: 39

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);
    }

Upvotes: 1

Related Questions