Loving Android
Loving Android

Reputation: 263

Android turning flash on returns NullPointerException with some Devices

so I made an app that reading text (using Google mobile vision OCR )

and i'm working with CameraSource class ..

i use this method to be able to turn the flash on/off

public void changeFlashStatus(View v) {




    Field[] declaredFields = CameraSource.class.getDeclaredFields();
    if (declaredFields != null) {
        for (Field field : declaredFields) {
            if (field.getType() == Camera.class) {
                field.setAccessible(true);
                try {
                    camera = (Camera) field.get(camSource);
                    if (camera != null) {
                        params = camera.getParameters();

                        if (!isFlash) {
                            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                            flash_btn.setImageResource(R.drawable.ic_action_flash_on);

                            isFlash = true;
                        } else {
                            params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                            flash_btn.setImageResource(R.drawable.ic_action_flash_off);
                            isFlash = false;

                        }
                        camera.setParameters(params);

                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

                break;
            }
        }
    }
}

it works prefectly with most devices

but lately i'm getting a lot of crash reports !

here's the report:

    Samsung Galaxy Note8 (greatlte), 6144MB RAM, Android 7.1
Report 1 of 2
java.lang.IllegalStateException: 

  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick (AppCompatViewInflater.java:293)

  at android.view.View.performClick (View.java:6308)

  at android.view.View$PerformClick.run (View.java:23969)

  at android.os.Handler.handleCallback (Handler.java:751)

  at android.os.Handler.dispatchMessage (Handler.java:95)

  at android.os.Looper.loop (Looper.java:154)

  at android.app.ActivityThread.main (ActivityThread.java:6823)

  at java.lang.reflect.Method.invoke (Native Method)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1563)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1451)
Caused by: java.lang.reflect.InvocationTargetException: 

  at java.lang.reflect.Method.invoke (Native Method)

  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick (AppCompatViewInflater.java:288)
Caused by: java.lang.NullPointerException: 

  at java.lang.reflect.Field.get (Native Method)

  at myApp.MyAppName.ReadingActivity.changeFlashStatus (ReadingActivity.java:412)

why it's not working with these devices and how i fix this ?

any solutions ?

Upvotes: 0

Views: 72

Answers (1)

Akshay
Akshay

Reputation: 6142

You would need two permissions Manifest.permission:

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

to be able to access the camera and the flashlight.

Note: Android 6.0 release includes the new APIs for accessing the camera’s flashlight.

Upvotes: 1

Related Questions