3
I am not able to activate the camera flash to use as flashlight. The code I am running took 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 added everything that is permission, but I also had no success:
<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" />
Someone would know how to solve, or some other way to do?
Thank you very much... I believe to be a problem with <uses-sdk android:minsdkversion="11" android:targetsdkversion="17"> .
– Maicon
Check here if it works.
– Lollipop
I couldn’t make it work. The error message shown is: java.lang.Runtimeexception: Unable to start Activity Componentinfo{com.teste.flash/com.teste.flash.flash}: java.lang.Runtimeexception: Fail to connect to camera service
– Maicon