0

I'm making a flashlight app. The app has an option to switch from rear camera flash to front camera flash. The problem is that, while the torch is on, if I switch the rear camera to front, the front camera flashes for like less than a second and then turns off. Sometimes this doesn't happen. So, this is a bug. I want the app to check if the torch remains on after the cameras were toggled. I think I have to use torch callbacks. But Google's documentation is not descriptive enough to understand.Also, not much discussion about this on SO. So, I have no idea how to use torch callbacks. I tried, I got errors... I must have tried something wrong. Here is my code: package com.danielfernandzz.flashlight;

import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraAccessException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
public boolean switchState = false;
Button switchbutton;
String cameraId;
CameraManager camManager;
Switch frontToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    switchbutton = (Button) findViewById(R.id.Switch);
    camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    frontToggle = (Switch) findViewById(R.id.frontToggle);
    try{cameraId = camManager.getCameraIdList()[0];}
    catch (CameraAccessException ex){finish();}
}

public void switched(View v){
    if(!switchState){
        switchState = true;
        switchbutton.setText("ON");
        try {camManager.setTorchMode(cameraId,true);}
        catch (CameraAccessException ex) {finish();}
    }
    else{
        switchState = false;
        switchbutton.setText("OFF");
        try {camManager.setTorchMode(cameraId,false);}
        catch (CameraAccessException ex) {finish();}
    }
}
public void frontFlash(View z){
    if (frontToggle.isChecked()){
        if(switchState) {
            try {camManager.setTorchMode(cameraId, false);}
            catch (CameraAccessException ex) {finish();}
        }
        try{cameraId = camManager.getCameraIdList()[1];}
        catch (CameraAccessException ex){finish();}
        if(switchState) {
            try {camManager.setTorchMode(cameraId, true);}
            catch (CameraAccessException ex) {finish();}
        }
        //I want to add the callback to take place here
    }
    else {
        if(switchState) {
            try {camManager.setTorchMode(cameraId, false);}
            catch (CameraAccessException ex) {finish();}
        }
        try{cameraId = camManager.getCameraIdList()[0];}
        catch (CameraAccessException ex){finish();}
        if(switchState) {
            try {camManager.setTorchMode(cameraId, true);}
            catch (CameraAccessException ex) {finish();}
        }
        //And here
    }
}

}

Any help would be appreciated. If you think that the error was being caused by a bug in my code then do tell me about that also. Any help would be appreciated.

VC.One
  • 14,790
  • 4
  • 25
  • 57
  • What are the errors or other output. What specifically are you not understanding? Try to ask a specific question based on what you've tried giving output to help people figure it out. – MrJLP Nov 01 '17 at 17:29
  • I don't see any errors in the logcat. I think this is a problem with some phones. The best option I think would be to check if the torch is on, if it isn't, turn it on again. – ThatDanielFernandes Nov 02 '17 at 03:09

0 Answers0