- Forum posts: 1
Feb 21, 2017, 6:07:25 PM via Website
Feb 21, 2017 6:07:25 PM via Website
when i press button in potrait mode Flash light is goes on and when I rotate to landscape mode flash light goes off..
Plz help me to solve this issue..
package com.example.admin.finalflash;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton imagebuttonon, imagebuttonoff;
public static Camera cmr;
Camera.Parameters parameters;
boolean isFlash = false;
boolean isOn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagebuttonoff = (ImageButton) findViewById(R.id.imagebuttonoff);
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
{
cmr = Camera.open();
parameters = cmr.getParameters();
isFlash = true;
}
//setting onclicklistner on off ie red imagebutton..
imagebuttonoff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlash)
// if your mobile support flash light
{
//if flash light is not on
if (!isOn) {
imagebuttonoff.setImageResource(R.drawable.onswitch); //replace offswitch image with onswitch image.
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);//also set on flash torch parameter..
cmr.setParameters(parameters);//by using object cmr.
cmr.startPreview();
isOn = true;// now flashlight is on..ie true
} else {
//exactly opposite as above ie as switch on..ie how to off flash light..
imagebuttonoff.setImageResource(R.drawable.offswitch);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cmr.setParameters(parameters);
cmr.stopPreview();
isOn = false; // now flash light is off..
}
} else { // if your mobile does not support flash light..
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Error"); // this will set title for error message...
builder.setMessage("Flash light not available"); // will display message
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {//so that you can click on "ok"..
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // so application is dismiss
finish();
}
});
AlertDialog alertDialog = builder.create(); //this will shows dialog box..which is showing above error message..
alertDialog.show();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
// on pause turn off the flash
cmr.stopPreview();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onResume() {
super.onResume();
// on resume turn on the flash
if (isOn)
cmr.startPreview();
}
@Override
protected void onStart() {
super.onStart();
// on starting the app get the camera params
cmr.startPreview();
}
@Override
protected void onStop() { // this will stop out flash light..
super.onStop();
if (cmr != null)
cmr.release();
cmr = null;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//outState.getBoolean("mode", false);
super.onSaveInstanceState(outState);
outState.putBoolean("mode", isOn);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
isOn = savedInstanceState.getBoolean("mode");
}
}
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.