Why is my flashlight app not working?

  • Replies:3
cececefotl
  • Forum posts: 2

Jun 23, 2016, 9:04:17 PM via Website

Hi, i've recently picked up android developing and I know the basics, so I figured I could follow some tutorials and make a simple flashlight app. I tried coding the app in several ways but I could never manage to turn on the LED.

Here is the MainActivity

public class MainActivity extends AppCompatActivity {
    ImageButton imageButton;
    Camera camera;
    Camera.Parameters parameters;
    boolean isFlash = false;
    boolean isOn = false;

    @Override
    protected void onStop() {
        super.onStop();
        if(camera != null){
            camera.release();
            camera = null;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);








        imageButton = (ImageButton)findViewById(R.id.imageButton);


        if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            camera = Camera.open();
            parameters = camera.getParameters();
            isFlash = true;
        }


        imageButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public  void onClick(View v) {

                if (isFlash) {

                    if (!isOn) {
                        imageButton.setImageResource(R.drawable.on);
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        camera.setParameters(parameters);
                        camera.startPreview();
                        isOn = true;
                    } else {
                        imageButton.setImageResource(R.drawable.off);
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                        camera.setParameters(parameters);
                        camera.stopPreview();
                        isOn = false;

                    }


                } else {

                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Error");
                    builder.setMessage("flash not supported");
                    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }

                    });

                    AlertDialog alertDialog = builder.create();
                    alertDialog.show();
                }

            }


            });

        }
}

Reply
Vladimir S.
  • Forum posts: 266

Jun 23, 2016, 9:30:46 PM via Website

Hello,

  1. Have you put required permissions in app's manifest? Look at this
  2. Not all of smartphones support FLASH_MODE_TORCH, try to use FLASH_MODE_ON, FLASH_MODE_AUTO or FLASH_MODE_RED_EYE.

Reply
cececefotl
  • Forum posts: 2

Jun 23, 2016, 10:20:50 PM via Website

Sure, I declared them like this

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.Camera" android:required="true"/>

and from settings>app>my app i can see that the app has been granted camera permissions. Later i'll try using another FLASH_MODE and i'll update the post.

EDIT: can't manage to get it working even with changing to FLASH_MODE_ON and _RED_EYE.

— modified on Jun 23, 2016, 11:39:26 PM

Reply
Vladimir S.
  • Forum posts: 266

Jun 24, 2016, 11:02:37 AM via Website

Maybe this will be helpfull.
Did you try to use another device for test?

Reply