0

I have read every single other post on this specific error, and this still does not make sense to me, at all.

So far I have tried:

import android.hardware.camera2.CameraManager;

public class HelloWorld {
    
    public int getcameracount() {
        try {
            return CameraManager.getCameraIdList().length;
        } finally {
            
        }
    }

    public void main(String[] args) {

System.out.println("number of cameras: " + getcameracount());

    }
}

And

import android.hardware.camera2.CameraManager;

public class HelloWorld {

    public void main(String[] args) {
        
        new cameraids list1;

System.out.println("number of cameras: " + list1.CameraIDList.length);

    }
}

class cameraids {
public String[] CameraIDList = CameraManager.getCameraIdList();
}

And

import android.hardware.camera2.CameraManager;

public class HelloWorld {

    public void main(String[] args) {
        
        cameraids list1 = new cameraids();

System.out.println("number of cameras: " + list1.CameraIDList.length);

    }
}

class cameraids {
public String[] CameraIDList = CameraManager.getCameraIdList();
}

And many, many others.

Without exception, I get

1. ERROR in /storage/emulated/0/redacted/redacted/src/HelloWorld.java (at line 19)
    String[] CameraIDList = CameraManager.getCameraIdList();
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Cannot make a static reference to the non-static method getCameraIdList() from the type CameraManager

As an error. Only the line number changes per attempt.

I've also looked at various code examples, and they all use some variation of

String[] idlist = manager.getCameraIdList();

Where idlist is any variable name, and manager is any variant of manager, Cmanager, or CameraManager depending on that example's author's setup. Mine is CameraManager.

The examples all seem to 'just work' for their respective authors.

I don't know what I'm doing differently. I know some assembly, C++, M and G code, basic etc, and they all seem really straightforward by comparison. Why can't I just print a number?

P.S. the filepath is redacted because it contained lots of swearing

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
db0htc
  • 31
  • 4

1 Answers1

2

As the error is explaining, the method getCameraIdList() within CameraManager is non-static, meaning you need an instance of CameraManager in order to access it.

Assuming you mean Android’s CameraManager class, this is how you might go about obtaining an instance of the CameraManager:

CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
// NOTE: You need an instance of 'Context' in order to access getSystemService()

You can then use cameraManager.getCameraIdList() as you have in your examples.

justis
  • 447
  • 7
  • 13