简体   繁体   中英

Flashlight won't turn on

Hi I followed I followed a tutorial here and wrote out the code, I tested it but the flashlight does not turn on and I cant figure out what's gone wrong. I left out the parts where there was sound because I preferred there not to be any.

package net.netne.benpaterson35;
import android.app.*;
import android.os.*;
import android.hardware.*;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity 
{

Button mainButton1;

private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mainButton1 = (Button) findViewById(R.id.mainButton1);

    hasFlash = getApplicationContext().getPackageManager().
        hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash) {
        AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
            .create();
        alert.setTitle("Sorry");
        alert.setMessage("This device doesn't have flash a flash light");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which){
                    finish();
                }
            });
            alert.show();
            return;
    }

    getCamera();
    mainButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            if (isFlashOn){
                turnOffFlash();}
        else {
            turnOnFlash();
        }

    };

});

}

private void getCamera(){
  if (camera == null){
    try {
        camera = Camera.open();
        params = camera.getParameters();
    } catch (RuntimeException e) {
        Log.e("Failed to open camera", e.getMessage());
    }

    }
 }
private void turnOnFlash() {
  if (!isFlashOn){
    if (camera == null || params == null){
        return;
    }
    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(params);
    camera.startPreview();
    isFlashOn = true;
}
}

private void turnOffFlash() {
    if (isFlashOn){
        if (camera == null || params == null) {
        return;
        }
     params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
      camera.stopPreview();
        isFlashOn = false;
        }
     }

@Override
protected void onDestroy()
    {
        super.onDestroy();
   }

@Override
  protected void onPause()
 {
      super.onPause();

     turnOffFlash();
  }

@Override
      protected void onRestart()
{
          super.onRestart();
}
  @Override
       protected void onResume()
{
                super.onResume();
               if(hasFlash)
                turnOnFlash();
}

@Override
protected void onStart()
 {
        super.onStart();
        getCamera();
  }

@Override
protected void onStop()
  {
        super.onStop();

    if (camera != null) {
    camera.release();
    camera = null;
    }
}
}

Thanks for reading :)

if you are working on new device the code you followed wont work on newer devices i made it work on Lollipop device by following this code give it a try

mCam = Camera.open();
Camera.Parameters p = mCam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCam.setParameters(p);
mPreviewTexture = new SurfaceTexture(0);
try {
   mCam.setPreviewTexture(mPreviewTexture);
} catch (IOException ex) {
   // Ignore
}
mCam.startPreview();

Did you add

<uses-permission android:name="android.permission.CAMERA" />

to your AndroidManifest?

Highly suspect you didn't add the permission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM