Jonathan Vukadinovic
Jonathan Vukadinovic

Reputation: 264

camera2 android camara callback camera device is null

So I'm using the camera2 api and when I open the camera:

          private void StartUpCam() 
      {
          CameraManager CM = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
          if(camId.compareTo("")!=0)
          {
              //Log.e("camera", "started");
              try{
                  Log.e("Start up Cam and camId:", camId);
                  CM.openCamera(camId, CDcallback, null);
              }
              catch(CameraAccessException e)
              {
                  Log.e("come with me", "if you want to live");
                  e.printStackTrace();
              }
          }
      }

As you can see I make sure the parameters needed to open the camera all check out and they do. Anyways so when the callback is hit:

           private CameraDevice CD;
      private CameraDevice.StateCallback CDcallback
        = new CameraDevice.StateCallback() {

            @Override
            public void onOpened(CameraDevice camera) {
                //Log.e("onOpened", "here");
                CD = camera;
                if(CD == null)
                    Log.e("onOpened", "CD is null");
                createCamPreviewSession();
            }

            @Override
            public void onError(CameraDevice camera, int error) {
                camera.close();
                CD = null;
            }

            @Override
            public void onDisconnected(CameraDevice camera) {
                camera.close();
                CD = null;
            }
        };

The Camera Device from the onOpened method always is null. I have no idea why this is happening. Any ideas?

Upvotes: 0

Views: 3319

Answers (2)

Pooja
Pooja

Reputation: 1

If you are integrating camera2api code into your project please add hardwareAccelerated=true in your manifest with that activity. It will work. In my case it is working.

<Application
    android:hardwareAccelerated="true"

Upvotes: 0

josua josh
josua josh

Reputation: 88

Have you tried camera2basic from github?

https://github.com/googlesamples/android-Camera2Basic

I have tried to study camera2 from there and from android developer android.hardware.camera2 reference. But it is soo confusing. I am using android studio 2, gradle 2.8, and nexus lg 5x android 6.

I copied the source codes to my project, check all errors on the IDE and found no error. After I tried it on my nexus, I always get the same errors on every line which calls CameraDevice class. It says null pointer on that class.

Then, I found this question here Android Camera2 Sample and it turns out the guy asking have already tried it with the method:

  1. File > New > Import Sample..
  2. Pick the Camera2Basic sample.
  3. Update your gradle to version 2.10 HOW?
  4. File > Project Structure... > on left tab click project > gradle
  5. Change the Gradle version to 2.10

Then I run the code to my Nexus 5X Android 6 and it runs really well. It has camera preview, It will ask permissions at first, It can save picture to storage, etc..

Upvotes: 1

Related Questions