22

When I call mCamera = Camera.open() it returns null, what could be causing this? My device is the Nexus 7.

I already have the permissions set in my AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />
Cheetah
  • 13,785
  • 31
  • 106
  • 190
  • Try [this](http://marakana.com/forums/android/examples/39.html) – Praveenkumar Aug 21 '12 at 11:41
  • Again, `camera = Camera.open();` returns null. – Cheetah Aug 21 '12 at 11:49
  • 4
    Yes...I'm a bit of an idiot though...the API reads `Creates a new Camera object to access the first back-facing camera on the device. If the device does not have a back-facing camera, this returns null.`...the Nexus 7 doesn't have a back facing camera, only a front one. – Cheetah Aug 21 '12 at 11:51
  • Yes. Before, seeing the device i didn't know this. Just know i know this through [the document](http://www.google.com/nexus/#/7) Try to open secondary camera. And, once just try [this](http://stackoverflow.com/q/9765935/940096) – Praveenkumar Aug 21 '12 at 11:52

4 Answers4

27

Figured it out,

You need to call Camera.open(0).

THIS IS ONLY VALID AND WORKING ON THE NEXUS 7 device, as it only has one camera, so is only useful if targeting that device only.

Cheetah
  • 13,785
  • 31
  • 106
  • 190
  • 2
    Also read [this](http://developer.android.com/reference/android/hardware/Camera.html#open%28int%29) – Praveenkumar Aug 21 '12 at 11:54
  • 5
    This is wrong answer. First, `0` is a hardcoded number. Second, as @Praveen mentioned, you should use both [getNumberOfCameras()](http://developer.android.com/reference/android/hardware/Camera.html#getNumberOfCameras()) and [open(int)](http://developer.android.com/reference/android/hardware/Camera.html#open(int)). –  Nov 17 '12 at 02:18
  • @LaiVung did you actually read all the post - I specifically mention that it's only valid for devices with one camera. – Cheetah Nov 20 '12 at 11:50
  • 2
    I know. But `0` is a hardcoded value. I'd avoid of using it. You can state my opinion is wrong, it's up to you. –  Nov 20 '12 at 11:53
  • 2
    LaiVung / user1521536 is correct. Do not hardcode camera id values. This answer is a broken shortcut that worked for one person in one instance. –  Dec 02 '15 at 15:38
  • 1
    Come on guys, how could a hardcoded value be the right answer to your problems? Start understanding the problems without using these damned shortcuts. – andrea.rinaldi Mar 16 '17 at 16:00
  • Using `open(0)` is perfectly legitimate. The [official doc](https://developer.android.com/reference/android/hardware/Camera.html#open%28int%29) says: *`cameraId` `int`: the hardware camera to access, between 0 and [getNumberOfCameras()](https://developer.android.com/reference/android/hardware/Camera.html#getNumberOfCameras())-1.* So, if the the app manifest **requires** "android.hardware.camera" feature, then cameraId=0 can be used. – Alex Cohn Dec 18 '17 at 08:52
5

For those who's testing an app on Android 6.0+, make sure that you have implemented Runtime Permission. Because simple permission in your AndroidManifest file is not enough.

1

I solved this issue by following below steps.

  1. Open the "AVD Manager" and select the virtual device you are using

  2. Click on the "Edit" button Click on edit button

  3. In the "Hardware" section, select "New" and add "Configures camera facing back" and click Ok.

  4. In the dropdown next to the entry, select "webcam0" or the one corresponding to the camera you want to use Select the camera you want to use

  5. Stop the AVD and restart again.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
Srijit
  • 475
  • 7
  • 30
0

If you build app above API 22, maybe you should request permission.

  • Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) Try to elaborate more to make your answer clearer and include more details. – Tomer Shetah Jan 14 '21 at 06:46