title: Android Turn on flashlight programmatically button
body: <p>I am working on an app in which I need to be able to turn on the Torch Light of an Android Phone by button press. I have written some code. However upon button press the light does not turn on. Below is the relevant code. Please note, this is being done within a fragment. I tried a standalone app with the same issues. </p>
  
  <pre><code>public class Flashlight extends Fragment implements View.OnClickListener {
      // in global
      private Camera camera;
      private Button button;
      Button FlashLightBTN
      // end global
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.udp_commands, container, false);
  
            FlashLightBTN = (Button) rootView.findViewById(R.id.flashlightBTN);
  
          Context context = getActivity().getApplicationContext();
          PackageManager pm = context.getPackageManager();
          if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
              // Log.e("err", "Device has no camera!");
              Toast.makeText(getActivity().getApplicationContext(), "Your device doesn't have camera!", Toast.LENGTH_SHORT).show();
  
              return;
          }
          camera = Camera.open();
          final Parameters p = camera.getParameters();
          FlashLightBTN.setTag(1);
          FlashLightBTN.setText("Turn Flashlight On");
          FlashLightBTN.setOnClickListener(new Button.OnClickListener() {
  
              public void onClick(View v) {
                  Button b = (Button) v;
                  String buttonText = b.getText().toString();
                  final int status = (Integer) v.getTag();
  
                  if (status == 1) {
                      p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                     camera.setParameters(p);
                  camera.startPreview();
  
                  FlashLightBTN.setText("Turn Flashlight Off");
                  v.setTag(0);
              }
              if (status == 0) {
                  p.setFlashMode(Parameters.FLASH_MODE_OFF);
                  camera.setParameters(p);
                  camera.stopPreview();
                  camera.release();                        FlashLightBTN.setText("Turn Flashlight On");
                      v.setTag(1);
                  }
  
              }
          });
          // other code
          return rootView;
      }
  }
  </code></pre>
  
  <p>Below is my manifest file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.andy_xmark1"
      android:versionCode="1"
      android:versionName="1.0"
      android:windowSoftInputMode="stateHidden" &gt;
  
      &lt;!-- Allows access to the flashlight --&gt;
      &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
      &lt;uses-feature android:name="android.hardware.camera.flash" /&gt;
      &lt;uses-feature android:name="android.hardware.camera" /&gt;
  
      &lt;uses-sdk
          android:minSdkVersion="10"
          android:targetSdkVersion="19" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.flashlight.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Logcat:</p>
  
  <pre><code>09-04 00:50:25.279: I/Adreno-EGL(11871): &lt;qeglDrvAPI_eglInitialize:320&gt;: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
  09-04 00:50:25.299: D/OpenGLRenderer(11871): Enabling debug mode 0
  09-04 00:50:25.329: D/dalvikvm(11871): GC_FOR_ALLOC freed 218K, 2% free 16986K/17236K, paused 11ms, total 11ms
  09-04 00:50:28.909: D/dalvikvm(11871): GC_FOR_ALLOC freed 387K, 3% free 17113K/17532K, paused 14ms, total 14ms
  09-04 00:50:33.659: D/dalvikvm(11871): GC_FOR_ALLOC freed 510K, 4% free 17117K/17660K, paused 15ms, total 15ms
  09-04 00:50:35.999: D/AndroidRuntime(11871): Shutting down VM
  09-04 00:50:35.999: W/dalvikvm(11871): threadid=1: thread exiting with uncaught exception (group=0x41cf2ba8)
  09-04 00:50:36.009: E/AndroidRuntime(11871): FATAL EXCEPTION: main
  09-04 00:50:36.009: E/AndroidRuntime(11871): Process: com.example.flashlight, PID: 11871
  09-04 00:50:36.009: E/AndroidRuntime(11871): java.lang.RuntimeException: Method called after release()
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.hardware.Camera.native_setParameters(Native Method)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.hardware.Camera.setParameters(Camera.java:1650)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at com.example.flashlight.flash$11.onClick(UDP_Commands.java:225)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.view.View.performClick(View.java:4438)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.view.View$PerformClick.run(View.java:18422)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.os.Handler.handleCallback(Handler.java:733)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.os.Handler.dispatchMessage(Handler.java:95)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.os.Looper.loop(Looper.java:136)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.app.ActivityThread.main(ActivityThread.java:5017)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at java.lang.reflect.Method.invokeNative(Native Method)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at java.lang.reflect.Method.invoke(Method.java:515)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  09-04 00:50:36.009: E/AndroidRuntime(11871):    at dalvik.system.NativeStart.main(Native Method)
  09-04 00:50:37.869: I/Process(11871): Sending signal. PID: 11871 SIG: 9
  </code></pre>
  
  <p>Any assistance or guidance on where I am going wrong would be greatly appreciative. Also if it matters, I am testing this with a Nexus 5. I have also downloaded a sample flashlight app from the playstore, and that works fine, so the hardware is good to go. </p>
  
  <p>Thanks in advance for </p>