Android: LED does not turn on / off

I am trying to write a small Android widget with a button that turns on / off the camera flashlight. I know there are thousands of it there, but I want to learn Android (and small steps seem to be the best way).

Now I have read the official documentation, some free tutorials on the Internet, and searched here on stackoverflow. As long as I don't get any errors, LogCat says that everything works as desired. But when I test the application on my Galaxy Nexus, the switch turns on / off, as it should, but the Cams LED does not turn on / off.

Here is my code (only those parts where the LED is really on / off):

if (isLightOn) { Log.d("receiver", "flashlight is on, disabling it"); if (camera != null) { param.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(param); camera.release(); camera = null; isLightOn = false; } } else { Log.d("receiver", "flashlight is off, enabling it"); camera = Camera.open(); if(camera == null) { Toast.makeText(context, R.string.no_camera, Toast.LENGTH_SHORT).show(); } else { // Set the torch flash mode param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); try { camera.setParameters(param); isLightOn = true; } catch (Exception e) { Toast.makeText(context, R.string.no_flash, Toast.LENGTH_SHORT).show(); } } } 

Any ideas why this is not working as intended?

+4
source share
1 answer

In general, look here . This is correctly explained.

EDIT:

What, by reference:

  • Enable

    camera = Camera.open (); Parameters p = camera.getParameters (); p.setFlashMode (Parameters.FLASH_MODE_TORCH); camera.setParameters (p); camera.startPreview ();

  • Disable

    camera = Camera.open (); Parameters p = camera.getParameters (); p.setFlashMode (Parameters.FLASH_MODE_OFF); camera.setParameters (p); camera.stopPreview ();

And add the following permission to AndroidManifest.xml.

PS This project was developed in Eclipse 3.7 and tested with Samsung Galaxy S2 (Android 2.3.3).

  • Android platform Button only.

File: res / layout / main.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/buttonFlashlight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:text="Torch" /> </RelativeLayout> 
  1. Activity Read the code, the button to turn on / off the flashlight, it should be clear.

     package com.mkyong.android; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class FlashLightActivity extends Activity { //flag to detect flash is on or off private boolean isLighOn = false; private Camera camera; private Button button; @Override protected void onStop() { super.onStop(); if (camera != null) { camera.release(); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.buttonFlashlight); Context context = this; PackageManager pm = context.getPackageManager(); // if device support camera? if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { Log.e("err", "Device has no camera!"); return; } camera = Camera.open(); final Parameters p = camera.getParameters(); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (isLighOn) { Log.i("info", "torch is turn off!"); p.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(p); camera.stopPreview(); isLighOn = false; } else { Log.i("info", "torch is turn on!"); p.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(p); camera.startPreview(); isLighOn = true; } } }); } } 
  2. Android permission Assign CAMERA permission.

File: AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mkyong.android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <application android:debuggable="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".FlashLightActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 
0
source

Source: https://habr.com/ru/post/1469274/


All Articles