Ezaan Saeed
Ezaan Saeed

Reputation: 13

App is Crashing When Turning on Flashlight

There Folks! I'm trying to a make flashlight app for android. When I press the flashlight on button the app crashes. I've tried many sites, seen their solutions and many Qs in Stack Overflow and none of them worked for me! Here is my App Code:

Java:

    public class FlashlightActivity extends Activity {

private Camera cameraObj;
ImageButton flashlight_switch;



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flashlight);
    flashlight_switch = (ImageButton) findViewById(R.id.flashlight_switch);

    flashlight_switch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)  {
            Camera.Parameters cameraParams =cameraObj.getParameters();
            cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            cameraObj.setParameters(cameraParams);
            cameraObj.startPreview();

            flashlight_switch.setImageResource(R.drawable.flashlight_switch_on);

        }
    });

    flashlight_switch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Camera.Parameters cameraParams = cameraObj.getParameters();
            cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            cameraObj.setParameters(cameraParams);
            cameraObj.stopPreview();

            flashlight_switch.setImageResource(R.drawable.flashlight_switch_off);


        }

    });


  }

}

Manifest:

<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera" />
<permission android:name="android.permission.FLASHLIGHT" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.germinai.ledflashlight.FlashlightActivity"
        android:label="@string/app_name"
        android:screenOrientation="nosensor" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="@string/orientation"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.germinai.ledflashlight.MainActivity$PlaceholderFragment"
android:background="@color/grey"
>

<ImageButton
    android:id="@+id/flashlight_switch"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:src="@drawable/flashlight_switch_off"
    android:background="@null"
    android:contentDescription="@null"
    android:hapticFeedbackEnabled="true"
    android:adjustViewBounds="false"
    android:layout_alignWithParentIfMissing="true"
    android:padding="20dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<ImageButton
    android:id="@+id/sos_switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/sos_off"
    android:background="@null"
    android:contentDescription="@null"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:adjustViewBounds="true"
    android:minHeight="12dp"
    android:minWidth="12dp"
    android:maxWidth="500dp"
    android:maxHeight="500dp" />

I tried my best not ask for help but now I can't really figure it out now. I'm a beginner so it would be very nice that you explain the answer in detail. Please ignore Any typos if there are. The App is tested on Galaxy S3 (4.4 kitkat) Galaxy Note 1 (4.0) P8 Lite (5.0.1) Galaxy Grand Prime (5.1.1 Lollipop) Droid Razr M (4.4 Kitkat) and many other devices, And it crashes on all of them!

Thanks In Advance!

Upvotes: 0

Views: 228

Answers (1)

Vishal Gaur
Vishal Gaur

Reputation: 658

FlashlightActivity.java

public class FlashlightActivity extends Activity {
ImageButton flashlight_switch;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flashlight);
    flashlight_switch = (ImageButton) findViewById(R.id.flashlight_switch);

    flashlight_switch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (Flash.getTorch()) {
                flashlight_switch.setImageResource(R.drawable.flashlight_switch_on);
            } else {
                flashlight_switch.setImageResource(R.drawable.flashlight_switch_off);
            }
        }
    });
}}

Flash.java

class Flash {
private static boolean flashOnOff = false;
public static Camera camera;
private static Camera.Parameters params;

public static boolean getTorch() {
    if (flashOnOff)  // turn off flash
        off();
    else              // turn on flash
        on();
    return flashOnOff;

}

private static void on() {
    if (!flashOnOff) {
        if (camera == null || params == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {
                int a = 10;
            }
        }
        try {
            params = camera.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.setPreviewTexture(new SurfaceTexture(0));
            camera.startPreview();
            flashOnOff = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

private static void off() {
    if (camera == null || params == null)
        return;
    params = camera.getParameters();
    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    flashOnOff = false;
}}

Upvotes: 2

Related Questions