ThatDanielFernandes
ThatDanielFernandes

Reputation: 11

How to check if camera flash is on programatically on Android Studio?

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.

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
    }
}
}

Upvotes: 1

Views: 590

Answers (1)

PAK-TECH
PAK-TECH

Reputation: 1

context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

See http://developer.android.com/reference/android/content/pm/PackageManager.html

Upvotes: 0

Related Questions