简体   繁体   中英

Flashlight Fragment Android Studio

I have created a flashlight fragment for my app and I have implemented this code and turning the flashlight on works, but i cant turn it off.

I tried to switch the light on and off from one button or like now from two but it made no difference.

When i switch the fragment, the light turns off.

I hope anyone knows what to do. Thx




public class Flashlight extends Fragment implements SurfaceHolder.Callback {

    private View view;
    Button button_On, button_Off;

    private boolean lightIsOn = false;


    Camera camera;
    android.hardware.Camera.Parameters parameters;

    @Override
    public void onStart() {
        super.onStart();
        SurfaceView preview = (SurfaceView) getView().findViewById(R.id.PREVIEW);
        SurfaceHolder mHolder = preview.getHolder();
        mHolder.addCallback(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        turnOffLight();
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             final Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_flashlight, container, false);

        button_On = (Button) view.findViewById(R.id.button_On);
        button_Off = (Button) view.findViewById(R.id.button_Off);


        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CAMERA,}, PackageManager.PERMISSION_GRANTED);
        }
        if (!getActivity().getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            Toast.makeText(getActivity(), "Device has no flashlight", Toast.LENGTH_SHORT).show();
        }


        button_On.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    turnOnLight();

            }
        });

        button_Off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                turnOffLight();
            }
        });

        return view;
    }

Turn On/Off Light:

    private void turnOnLight() {
        if (!lightIsOn) {
            if (camera == null || parameters == null) {
                return;
            }
            parameters = camera.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(parameters);
            camera.startPreview();
            lightIsOn = true;
        }
    }

    private void turnOffLight() {

        if (lightIsOn) {
            if (camera == null || parameters == null) {
                return;
            }

            parameters = camera.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(parameters);
            camera.stopPreview();
            lightIsOn = false;
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (camera != null) {
            camera.stopPreview();
            camera.setPreviewCallback(null);
            camera.release();
            camera = null;
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (camera == null) {
            camera = Camera.open();
            parameters = camera.getParameters();
            try {
                camera.setPreviewDisplay(holder);
            } catch (IOException e) {
                camera.release();
                camera = null;
            }
        }
    }

}

Manifest:

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

XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Flashlight">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <SurfaceView
            android:id="@+id/PREVIEW"
            android:layout_width="1dp"
            android:layout_height="1dp"
            android:layout_marginBottom="730dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/button_On"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="on" />

            <Button
                android:id="@+id/button_Off"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="off" />

        </LinearLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

Okay update the permission in manifest file as below:

<permission android:name="android.permission.FLASHLIGHT"
             android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
             android:protectionLevel="normal"
             android:label="@string/permlab_flashlight"
             android:description="@string/permdesc_flashlight" />

Try to off the light using below code:

cam.stopPreview();
cam.release(); // Very Important line

In which line?

private void turnOffLight() {

        if (lightIsOn) {
            if (camera == null || parameters == null) {
                return;
            }
            camera = Camera.open();

            parameters = camera.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(parameters);
            camera.stopPreview();
            camera.release(); // Very Important line
            lightIsOn = false;
            button_OnOff.setText("On");

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM