0

I wanted to build a flashlight app using the following code. It's working on a friend's HTC Desire HD, but it isn't on my RAZR and a friend's Galaxy Nexus. I also tried the solution with focus_mode_infinity, but there's still no success.

package com.example.flashlight;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

Camera camera = null;
Parameters parameters;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



     final Button OnOff = (Button)findViewById(R.id.Switch);


    OnOff.setOnClickListener(new Button.OnClickListener() {




@Override   
public void onClick(View arg0) {



    if(camera == null) {
        camera = Camera.open();
        camera.startPreview();
        parameters = camera.getParameters();
        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
        camera.setParameters(parameters);

    }
    else {
        parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        camera.setParameters(parameters);
        camera.release();
        camera = null;

    } 

} 


});
}}

2 Answers2

0

I think FLASH_MODE_TORCH is not supported by RAZR, I've had the same issue with a customer reporting the same problem for one app (Flash not flashing). What I suggest you is before setting the parameter to check if its supported:

List<String> flashModes = parameters.getSupportedFlashModes();
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
    // Mode supported good to go
}
Wizche
  • 893
  • 13
  • 32
  • This will not work. Example: Galaxy S3 mini, supported flash modes: [auto, macro, fixed]. Still FLASH_MODE_TORCH and FLASH_MODE_OFF work fine. – Cynichniy Bandera Sep 18 '13 at 17:46
0

after setting parameters of the camera use the below method:

camera.startPreview();

here camera is your Camera object.