I can not activate the camera flash to use as a flashlight. The code I'm running has taken from tutorials on the internet.
package com.getten.home;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class Login extends ActionBarActivity {
private Toolbar pToolbar;
private Camera mCamera;
private boolean lFlashLigado = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
pToolbar = (Toolbar) findViewById(R.id.toolbar);
pToolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(pToolbar);
try {
mCamera = Camera.open();
} catch (Exception ex) {
Log.e("Camera", "Impossível ouvir a câmera!");
}
}
@Override
protected void onPause() {
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.lanterna) {
ligaDesligaFlash(lFlashLigado);
}
return true;
}
private void ligaDesligaFlash(boolean lDesliga) {
if (lDesliga) {
if (mCamera != null) {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.startPreview();
lFlashLigado = false;
}
} else {
if (mCamera != null) {
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
lFlashLigado = true;
}
}
}
}
I am compiling the application in version "22". I have already added all that is allowed, but I have not been successful either:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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.autofocus" />
Would anyone know how to solve it, or some other way?