0
\$\begingroup\$

One of my first projects. I present to you - Android Flashlight app!

public class MainActivity extends AppCompatActivity {
    Camera cam = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onBtn(View view) {
        if (checkCameraHardware(this)){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
               if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
                    handleFlash();
               }else {
                   handleFlash();
                   System.out.println("Check : has permission");
               }
            }
        }
    }
    public void handleFlash() {
            if (cam == null){
                cam = getCameraInstance();
            }
            Camera.Parameters parameters = cam.getParameters();
            if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_OFF)) {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                cam.setParameters(parameters);
                cam.startPreview();
            } else if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH)) {
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                camEnd();
            }
    }

    public static Camera getCameraInstance(){
        Camera camera = null;
        try {
            camera = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
            e.printStackTrace();
        }
        return camera; // returns null if camera is unavailable
    }
    private boolean checkCameraHardware(Context context) {
        if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            return true;
        } else {
            return false;
        }
    }

    public void camEnd() {
        cam.release();
        cam = null;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        camEnd();
    }
}

I have tested this on my Marshmallow device and it works as expected (but no on my lollipop device for some reason). Any suggestions would be great!

\$\endgroup\$
2
\$\begingroup\$

Your inner if-else is incorrect, and that's why it's not working under Marshmallow.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        System.out.println("Check : has permission");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        handleFlash();
    }
} else {
    handleFlash();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.