Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flash torch functionality not working through app-widget in nexus 5

Flash is turning-on fine from application (added surface_view to the layout), when I try to turn-on flash through app-widget it's not working. I used Camera and SurfaceView Here is the code I am using

Camera mCamera;
SurfaceView preview;
mCamera = Camera.open();
mCamera.setPreviewDisplay(preview.getCameraHolder());
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);  
mCamera.startPreview();

Added permission and features in Manifest are:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" android:required="false" />
like image 364
Sruthi Mamidala Avatar asked Feb 14 '23 00:02

Sruthi Mamidala


1 Answers

The camera needs a surface to cling to in order to open the
Flashlight..however SurfaceView cannot be applied to a widget. So this is what you need to.....

Add this to your turnFlashOn code:

try {
mCamera.setPreviewTexture(new SurfaceTexture(0));
} catch (IOException e) {
e.printStackTrace();
}

Also, remove all references to surfaceview in your code as it is not applicable in a widget

like image 171
SuperStallion Avatar answered Apr 08 '23 21:04

SuperStallion