How to turn off Flash light of Camera through intents in Android Programatically?

烈酒焚心 提交于 2019-12-24 00:33:12

问题


How to turn off Flash light of Camera through intents in Android Programatically?

I use the below code for activating Camera application and then taking the picture and get back to my application.

String packageName = takePictureIntent.resolveActivity(packageManager).getPackageName();
if (mPhotoFileUri != null)
      getActivity().grantUriPermission(packageName, mPhotoFileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoFileUri);

startActivityForResult(takePictureIntent, TAKE_PICTURE);

I want to turn off Camera flash when its open by default.How its possible?

Suggestions will be appreciated:-)


回答1:


to turn on/off the phone camera led or flashlight in Android, you can use following :

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

And, not forgot to put following permission on AndroidManifest.xml.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

Edit-1:

Unfortunately, when using the camera with Intent, the only extra parameter you can set is

MediaStore.EXTRA_OUTPUT

I'm guess you can't change via Intent. Reference from this question




回答2:


For this you should do like :

1) Check whether flash light is available or not ?

2) If yes then Turn Off/On

3) If no then you can do whatever according to your app. needs

For Checking availability of flash in device:

You can use the following

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

which will return true if a flash is available, false if not.

See link for more information.

For turning on/off flashlight :

I googled out and got this about android.permission.FLASHLIGHT. Android manifests' permission looks promising:

<!-- Allows access to the flashlight -->
 <permission android:name="android.permission.FLASHLIGHT"
             android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
             android:protectionLevel="normal"
             android:label="@string/permlab_flashlight"
             android:description="@string/permdesc_flashlight" />

Then make use of Camera and set Camera.Parameters. The main parameter used here is FLASH_MODE_TORCH.

eg.

Code Snippet to turn on camera flash light.

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

Code snippet to turn off camera led light.

  cam.stopPreview();
  cam.release();

Issues :

There are also some problems while turning On/Off flashlight. eg. for the devices not having FLASH_MODE_TORCH or even if it has, then flashlight doesnot turn ON etc.

Typically Samsung creates alot of problems.

You can refer about problems in the given below list:

1) Use camera flashlight in Android

2) Turn ON/OFF Camera LED/flash light in Samsung Galaxy Ace 2.2.1 & Galaxy Tab

REFERENCE LINK : How to turn on camera flash light programmatically in Android?



来源:https://stackoverflow.com/questions/33251960/how-to-turn-off-flash-light-of-camera-through-intents-in-android-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!