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" />
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
来源:https://stackoverflow.com/questions/21284473/flash-torch-functionality-not-working-through-app-widget-in-nexus-5