I am developing an app which needs to open the flashlight of my Galaxy Nexus device. I have referred to the post here
LED flashlight on Galaxy Nexus controllable by what API?
public class TestCamera extends Activity implements SurfaceHolder.Callback{
Camera mCamera;
public static SurfaceView preview;
public static SurfaceHolder mHolder;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preview = (SurfaceView) findViewById(R.id.camSurface);
mHolder = preview.getHolder();
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Button onLEDbtn = (Button) findViewById(R.id.onLED_btn);
onLEDbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
}
});
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mHolder = holder;
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.stopPreview();
mHolder = null;
}
}
Manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
However, I still cant switch on the flashlight. Could anyone point out my errors? Thanks
To use the flashlight, open the Quick settings panel by swiping down from the top of the screen using two fingers. Next, tap the Flashlight icon to turn the light on or off.
With Samsung's Android 10 update, you can add the flashlight as a lock screen shortcut. Open the Settings app and go to "Lock screen," then "Shortcuts." Choose which side you want the shortcut on by hitting either "Right shortcut" or "Left shortcut," then pick "Flashlight" towards the top of the next screen.
Turn on the Flashlight with the Volume Buttons You can even enable the Flashlight from your lock screen, but you will need to go into the app's settings to allow that. To access Settings, just open the app and tap on the three vertical dots on the top right of your display.
you must to set call back mHolder.addCallback(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With