Flashlight app crash within secs of icon clicked [closed] - android

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have created simple flashlight app. There isn't any error at compile. but when the icon is clicked it just stop unexpectedly. I have tried in both physical android phone and emulator. both comes up with same result. I cant see any problem in the code. can someone recify this problem.
Flashlight.java (Activity)
package flashlight.turnmeyon.crystal.laser;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class Flashlight extends AppCompatActivity {
ImageButton imagebutton;
Camera camera;
Camera.Parameters parameters;
boolean isflash = false;
boolean ison = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_flashlight);
imagebutton = (ImageButton) findViewById(R.id.buttonOn);
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
camera.open();
parameters = camera.getParameters();
isflash = true;
}
imagebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isflash) {
if (!ison) {
imagebutton.setImageResource(R.drawable.buttonon);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
ison = true;
} else {
imagebutton.setImageResource(R.drawable.buttonof);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
ison = false;
}
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(Flashlight.this);
builder.setTitle("Error...........");
builder.setMessage("Flashlight is not avaliable");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
AlertDialog alertdialog = builder.create();
alertdialog.show();
}
}
});
}
#Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
camera = null;
}
}
}
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#757575"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="#+id/buttonOn"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#757575"
android:src="#drawable/buttonof"
android:scaleType="fitCenter"
android:layout_marginTop="80dp"
android:layout_marginRight="80dp"
android:layout_marginLeft="80dp"
android:layout_marginBottom="40dp"
android:layout_centerVertical="true"
/>
</RelativeLayout>

Here is reference for you.
I created Flash Example. Please review below code.
public class MainActivity extends AppCompatActivity {
ImageView switch_on_off;
private Camera camera;
Parameters params;
public boolean isSupportFlash, isFlashLighOn;
String status;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isSupportFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!isSupportFlash) {
Toast.makeText(getApplicationContext(), "Your device does not support flash. ", Toast.LENGTH_LONG).show();
}
switch_on_off = (ImageView) findViewById(R.id.switch_on_off);
connectCameraService();
switch_on_off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlashLighOn) {
offFlashLight();
switch_on_off.setImageResource(R.drawable.switch_off);
} else {
onFlashLight();
switch_on_off.setImageResource(R.drawable.switch_on);
}
}
});
}
public void onFlashLight() {
if (!isFlashLighOn) {
status = "ON";
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashLighOn = true;
switch_on_off.setImageResource(R.drawable.switch_on);
}
}
public void offFlashLight() {
if (isFlashLighOn) {
status = "ON";
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashLighOn = false;
switch_on_off.setImageResource(R.drawable.switch_off);
}
}
public void connectCameraService() {
if (camera == null) {
camera = android.hardware.Camera.open();
params = camera.getParameters();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
offFlashLight();
}
#Override
protected void onPause() {
super.onPause();
offFlashLight();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
/*if (isSupportFlash) {
onFlashLight();
}*/
}
#Override
protected void onStart() {
super.onStart();
connectCameraService();
}
#Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
camera = null;
}
}
}
Required permission
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

Related

Code to remain flashlight when activity is on paused and screen lock is pressed

i am implementing flashlight, and i have called all the functions i,e onDestroy() onBackpressed() to remain my flash light on. but i have bug that if i pressed on back and after that screen is locked the flashlight switches off after few seconds. kindly help me with the code.
public class MainActivity extends AppCompatActivity {
ImageButton btnSwitch;
Camera camera;
Camera.Parameters parameters;
boolean isflash = false;
boolean ison = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH));
{
try {
camera = Camera.open();
parameters = camera.getParameters();
isflash = true;
}catch (Exception e ){
Log.e(getString(R.string.app_name), "failed to open Camera");
e.printStackTrace();
}
}
btnSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isflash)
{
if (!ison)
{
btnSwitch.setImageResource(R.drawable.off);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
ison = true;
}
else {
btnSwitch.setImageResource(R.drawable.btn_switch_on);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
ison = false;
}
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Error.....");
builder.setMessage("FlashLight is not available on this device....");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
});
}
#Override
protected void onStop() {
super.onStop();
if(!ison){
if (camera!=null)
{
camera.release();
camera = null;
}
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(!ison){
if (camera!=null)
{
camera.release();
camera = null;
}
}
}
#Override
protected void onPause() {
super.onPause();
if(!ison){
if (camera!=null)
{
camera.release();
camera = null;
}
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(!ison) {
if (camera != null) {
camera.release();
camera = null;
}
}
}
}

Not Working Properly Orientation of Flashlight Android Application

I am creating Flashlight Android App..and facing below problem..Please guide me ...
when i press button in potrait mode Flash light is goes on and when I rotate to landscape mode flash light goes off..
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;
}
imagebuttonoff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlash)
{
//if flash light is not on
if (!isOn) {
imagebuttonoff.setImageResource(R.drawable.onswitch); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cmr.setParameters(parameters);
cmr.startPreview();
isOn = true;
} else {
imagebuttonoff.setImageResource(R.drawable.offswitch);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cmr.setParameters(parameters);
cmr.stopPreview();
isOn = false;
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Error");
builder.setMessage("Flash light not available");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
cmr.stopPreview();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
if (isOn)
cmr.startPreview();
}
#Override
protected void onStart() {
super.onStart();
cmr.startPreview();
}
#Override
protected void onStop() {
super.onStop();
if (cmr != null)
cmr.release();
cmr = null;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.getBoolean("mode", false);
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
}
I have a solution for this, which will also be helpful for others who are interested in creating a Flashlight Android Application, as described on my blog.
I just entered these lines in the Manifest.xml file.
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize">

Android Camera: Turn on/off camera flash - Application crash when rotate device

I have a simple application with a button to turn on/off camera flash:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.btnFlash);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(!isLight)
{
switchON();
}
else
{
switchOFF();
}
}
});
}
If the flash was off, it will turn on and else, turn off. Yes, it works well.
The problem is:
- Firstly, I pressed the button to turn on, after that, I rotate my device and then pressing again to turn off -> Application crash.
Fatal Exception: main - Runtime Exception: Fail to connect to camera
service
These are 2 functions to turn on/off
public void switchON()
{
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLight = true;
}
public void switchOFF()
{
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
camera.release();
isLight = false;
}
Try This (I re-wrote the code):
public class YourClass extends Activity {
private Button button;
private Camera camera;
private boolean isLight=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.btnFlash);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(!isLight)
{
switchON();
}
else
{
switchOFF();
}
}
});
}
#Override
protected void onResume() {
super.onResume();
try{
camera = Camera.open();
} catch( Exception e ){
e.printStackTrace();
}
}
#Override
protected void onPause() {
if( camera != null ){
camera.release();
camera = null;
}
super.onPause();
}
private void switchOFF(){
if( mCamera != null ){
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
isLight = false;
}
}
private void switchON(){
if( mCamera != null ){
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isLight=true;
}
}
}
Try:
public void switchOFF()
{
if(camera == null)
{
camera = Camera.open();
}
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
camera.release();
isLight = false;
}

Camera flashlight project has stopped while clicking

I am working with camera flashlight project.
It load on the emulator but when I click on the "On" button it has stopped unexpectedly. Here is the cat-log https://dl.dropbox.com/u/15065300/logcat1.png
Here is the code:
public class FlashLight extends Activity {
private final static String LOG_TAG = "FlashLight";
private Button mOnBtn;
private Camera mCamera;
private boolean isActive;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//this will be inside your onCreate...
mOnBtn = (Button) findViewById(R.id.on_btn);
mOnBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
flipSwitch();
processClick();
}
});
}
//these will be outside your onCreate
public void flipSwitch() {
isActive = !isActive;
}
#Override
protected void onResume() {
super.onResume();
try{
mCamera = Camera.open();
mCamera.startPreview();
Toast.makeText(getApplicationContext(),"Camera is present", Toast.LENGTH_LONG).show();
} catch( Exception e ){
Log.e(LOG_TAG, "Impossible d'ouvrir la camera");
}
}
#Override
protected void onPause() {
if( mCamera != null ){
mCamera.release();
mCamera = null;
}
super.onPause();
}
public void processClick() {
if(isActive) {
Parameters params = mCamera.getParameters();
params.set("flash-mode", "torch");
mCamera.setParameters( params );
mCamera.startPreview();
}
else {
Parameters params = mCamera.getParameters();
params.set("flash-mode", "off");
mCamera.setParameters( params );
mCamera.stopPreview();
}
}
}
You have a NullPointerException on line 74 of Flashlight.java in your code.
It's probably that mCamera or params is being used while it's value is null. Check the initializations/assignments to these values.

Android Camera flash not turning on after turned off

I have a button on my camera preview screen to toggle the camera's flash. The camera starts on auto-flash which works then when the button is pressed the flash turns off but when i try to turn the flash back on it doesn't turn on and i don't know why?
Log.d("flash",mCamera.getParameters().getFlashMode());
Displays on off and auto as i press it. But it doesn't turn back on. Here is my full code
public void flashPressed(View v)
{
ImageButton flashButton = (ImageButton)findViewById(R.id.flash);
Camera.Parameters myP = mCamera.getParameters();
if(flashOn == 0)
{
flashButton.setImageResource(R.drawable.device_access_flash_off);
myP.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
flashOn = 1;
}
else if(flashOn == 1)
{
myP.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
flashButton.setImageResource(R.drawable.device_access_flash_on);
flashOn=2;
}else{
myP.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
flashButton.setImageResource(R.drawable.device_access_flash_automatic);
flashOn =0;
}
mCamera.setParameters(myP);
Log.d("flash",mCamera.getParameters().getFlashMode());
}
This is how I used to create a Flash app.
public class Flash extends Activity {
boolean cameraOpened;
static Camera camFlash = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onPause() {
super.onPause();
if (camFlash != null) {
camFlash.stopPreview();
camFlash.release();
camFlash = null;
}
}
public void turnOnFlash(View view) {
if (camFlash == null) {
camFlash = Camera.open();
}
if (camFlash != null) {
Parameters params = camFlash.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camFlash.setParameters(params);
camFlash.startPreview();
camFlash.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
}
public void turnOffFlash(View view) {
if (camFlash != null) {
camFlash.stopPreview();
camFlash.release();
camFlash = null;
}
}
}
the methods turnOnFlash() and turnOffFlash are called from xml android:onClick.
To avoid this problem I just created a new camera each time the flash option changes

Categories

Resources