I making a Flashlight app, and I use Fragments. When I press the button, The lantern light delayed more than 4 seconds, and i don't know what happen. Also, when I Press the switch button another time, the Flashlight doesn't turn off Any idea?
Also I would like to make a stroboscopic lantern light with another button.
I search in internet but i dont find another option to make this feature, only this.
this is my code
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
public class HerramientasFragment extends Fragment {
private Camera cam;
private Switch linterna;
public HerramientasFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
cam = Camera.open();
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View masterView = inflater.inflate(R.layout.fragment_herramientas, container, false);
linterna = (Switch) masterView.findViewById(R.id.switch_linterna);
linterna.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Switch liternaSwitch = (Switch) v;
Parameters p;
if (liternaSwitch.isChecked()) {
p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
} else {
p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
cam.stopPreview();
}
}
});
return masterView;
}
}
It is possible that the 4 second delay is hardware/operating system related and out of your control. That's not to say it can't be fixed, but I am unable to find anything related to it (Some Android experts might have a better idea here).
The light not turning off is probably because you need to add cam.release();
as mentioned in this answer.
As for a stroboscopic light, I found this tutorial. It seems to be almost exactly what you are looking for.
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