Unable to use the flashlight in Galaxy Nexus

2019-02-23 09:17发布

问题:

I have to following code and I just want to open the flashlight in my Galaxy Nexus(4.0.2) and it failed to do so.

public class welcome extends Activity {

//MediaPlayer player;
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);

    Camera cam = Camera.open();     
    Parameters p = cam.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);
    cam.startPreview();



}
}

My Manifest file:

<uses-sdk android:minSdkVersion="12" />
       <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />

I would like to ask if it is my coding error or android version problem? Thanks

回答1:

The galaxy nexus is unable to use the flash without having an attached surfaceView and surfaceHolder. I have a Nexus myself, and after attaching the preview-view it worked. Since you dont want to see what the camera sees in a flashlight app, just make it 1x1 pixel.

Source: LED Flashlight does not work on Samsung Galaxy Nexus



回答2:

Have you tried the code here: http://android-er.blogspot.co.uk/2011/02/control-flash-light-function-as-torch.html

It definitely works on a rooted Desire running Cyanogenmod 7 and on an ICS rom (4.0.3) for me. Perhaps try running this and see if it works, and then proceed from there?

It also includes checks to see if it can find the camera, then open the camera. There seems to be no need for the StartPreview().

The order of events seems different:

1: If camera is present, open camera 2: Set parameters 3: parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
4: camera.setParameters(parameters);



回答3:

Use surface view to attach camera as some of the devices needs surface view. I am sure this will work a link! for you