Just getting into Android programming and this is my first app. It is essentially a toggle button which turns the light on or off. The app loads fine, if I press the button it turns on fine, however if I turn it off while it is on I get "App has stopped working" message and it closes.
Can someone point where I'm going wrong? Thanks
public class MainActivity extends ActionBarActivity {
int flag = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.editText);
if (flag==0) {
flag=1;
editText.setText("ON");
Camera camera = Camera.open();
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
}
else {
if (flag == 1) {
flag = 0;
editText.setText("OFF");
Camera camera = Camera.open();
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
}
}
}
});
}
}
First off, when you get that it means you crashed. When that happens, a stack trace and exception type will be in your logcat. This will tell you how to debug.
However in this case I can tell you what happened. When you call Camera.open(), you are receiving a camera object that you own until you release it. You need to save this, and release it when your app is done with it (when it exits). So in your off code, when you call Camera.open again it returns null, because you already own the camera and it can't give it to you again.
Actually you need to account for Camera.open returning null any time you call it- another app may have requested the camera, in which case the OS can't give it to you and returns null.
Related
So for my Android app, I have created a SurfaceView and assigned it as camera preview and start camera preview using the necessary API's. But now I want to turn the Flashlight ON (kind of act like a Torch) while the preview is working.
Please note that I have seen tons of examples online on how to turn flashlight on and it works as long as I don't call the open camera API. Below is the code -
try
{
CameraManager cameraManager = (CameraManager)context.GetSystemService(Context.CameraService);
if (cameraManager != null)
{
//for the sake of brevity, hardcoded the camera id. 0 is mostly back camera
cameraManager.SetTorchMode(0, true);
}
}
catch (CameraAccessException e)
{
LogUtil.Error("CameraInput", e.ToString());
}
Please note I am testing on Android N and hence the above code works flawlessly. But as soon as I call below line of code, the flash turns off.
Camera camera = Camera.Open(0);
// ...... some code ....//
camera.StartPreview();
When the above 2 lines execute, the flash goes off. Is this a know behaviour like where camera takes exclusive lock over flash hardware and resets it's value to default.
I tried reversing the above code i.e calling the Camera Open API being called first and then setting the flash. On that I get CameraAccessException , camera already in use.
What am i missing ?
Try this while previewing on SurfaceView
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);
Remember that If you want to use it as flashlight you can do:
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
If not, to turn flash on which will come out when you take the picture, you use:
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
Happy Coding!
Figured out how to torch on and off while camera is previewing. On button click to on/off the light, use this code:
//at some other function where camera is initialised and start preview
//...
Camera camera = Camera.open();
camera.startPreview();
//...
boolean lightOn = false;
//...
buttonLight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Camera.Parameters p = camera.getParameters();
if (!lightOn) {
lightOn = true;
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
} else {
lightOn = false;
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
}
}
});
Happy Coding! :D
I am using the camera flash for a Morse code application. I create a new camera object when the class is created. The user has a button which is used to reset and also release the camera when required (If they want to stop the light Morse sequence prematurely).
The problem is that when they hit the reset button because the activity is not created or loaded again the camera never get reinitialized - this is a problem because the method of the class that it is calling is used by another class and releases the camera when it is has completed it's function. I am not sure how to structure the code in a way that allows me to do this.
I am wondering if anyone has any advice/suggestions in how to achieve this?
//Camera object being declared
Light light;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__morse);
//Camera object being initialized
light = new Light
}
//Reset user pressed a button
public void reset(View view)
{
light.release();
}
//Releasing the camera
public void release() {
if(camera != null)
{
camera.stopPreview();
camera.release();
camera = null;
}
}
Don't initialize the camera in onCreate. Do it in onStart, and release it in onStop.
I am making an android flashlight app. I it has two activities, a main activity and a settings activity where the camera led can be toggled on and off. It also has another class where all the camera changes are handled like opening it, releasing it, and turning the light on and off.
I kept getting errors when turning on the led based on shared preference settings because I was not opening and releasing the camera in the correct activity lifecycle stages. I fixed the issues by releasing the camera when onPause is called in either activity and turning the led on or off (based on shared preference settings) when onResume is called in either activity.
The problem I am having now is that if the led is on, it turns off briefly when switching from one activity to the other because I have to release the camera and then open again in the new activity. Can anyone help me figure out a way to eliminate this problem? Where should I open and release the camera? I have tried releasing it in on destroy but the led stays on when the app is minimized to the background, which is undesirable. Thanks for any suggestions.
In both main and settings activity I have:
#Override
protected void onResume() {
super.onResume();
if (sp.getBoolean("LED_TOGGLE_CB", false) == true) {
flash.turnFlashOn();
}
}
#Override
protected void onPause() {
super.onPause();
flash.killCamera();
}
In the Flash class I have:
void getCamera() {
if(camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch(RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
void turnFlashOn() {
getCamera();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
public void killCamera() {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
i have a little problem (or a big ^^), i've created a custom camera activity for an android app and when i try to activate the flash, the view is freezed :( , but when the activity is launched all is alright .
This is my way to activate the flash
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
FlashActivation.setVisibility(View.VISIBLE);
FlashActivation.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (inPreview) {
camera.stopPreview();
}
// NB: if you don't release the current camera before
// switching, you app will crash
camera.release();
camera = Camera.open(currentCameraId);
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(p);
camera.startPreview();
}
});
}
If some body can solved the problem it's with pleasure that i accept his solution :D.
Thank you
if you want to activate flash
There is no need to stop the preview and restart it.
And there is also no need to release the camera.
you can use this
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
camera.setParameters(parameters);
In my application i have an image button that basically switches on and off the flash LED. The code is running fine for the first time i.e. On first Click it Switches On the LED and on the Second Click it Switches it Off. But then Nothing happens third Click onwards. I am testing this on Nexus S.
Following is the Code for the ImageButton Click Method.
public void ToggleTorch(){
final ImageButton tt = (ImageButton)findViewById(R.id.tt);
tt.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
if (isFlashOn){
mycam.stopPreview();
isFlashOn = false;
} else {
mycam.startPreview();
isFlashOn = true;
}
}
});
}
From what i think, it has to do something with the SurfaceView as i think it is not being destroyed while calling stopPreview but i am not sure..
Following is the Code for the onCreate Method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Check if Flash Light is Available
Boolean has_flash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(has_flash){
setContentView(R.layout.activity_main);
SurfaceView preview = (SurfaceView)findViewById(R.id.pSv);
SurfaceHolder holder = preview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
disableSleepMode();
initFlashLight();
ToggleTorch();
screenTorchOn();
} else {
setContentView(R.layout.activity_main);
disableSleepMode();
screenTorchOn();
}
}
any help would be appreciated. Thanks.
instead of calling stop preview release camera and make camera instance null. To restart camera, initialize camera again. Make two different method one for initialize camera and another to release it. This will solve your problem.