100

I'm connecting a webcam to my emulator by setting the front camera to "webcam0" in the AVD Manager. When I start the emulator's camera application, I get the error

CameraService::connect X (pid 702) rejected (invalid cameraId 0).

Here's the relevant portion of the Android source code:

sp<ICamera> CameraService::connect(
    const sp<ICameraClient>& cameraClient, int cameraId) {

    int callingPid = getCallingPid();

    [...]

    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
        LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
            callingPid, cameraId);
        return NULL;
    }

    [...]
}

The webcam has been correctly assigned an ID of 0 because there's only 1 camera. However, mNumberOfCameras is presumably still 0. This means that the camera is being registered by the emulator, but it hasn't bothered to update the number of connected cameras.

How can I connect a webcam so that it will be properly recognized by the emulator?

Edit: the command emulator -webcam-list -avd <name of your AVD> in \android-sdks\tools gives the result:

List of web cameras connected to the computer:
Camera `webcam0` is connected to device `AndroidEmulatorVC0` on channel 0 using pixel format `BGR4`

When I launch the webcam from Eclipse's AVD manager or using emulator -camera-front webcam0 -avd <name of your AVD>, I get the following window:

enter image description here

Edit 2: This seems to be a bug in the emulator. The suggested answer tells you what to do to set up the camera, but doesn't solve the problem for me. I ultimately solved it by using a laptop with a built-in webcam. Perhaps another USB webcam might have worked as well.

1''
  • 25,192
  • 30
  • 134
  • 195

3 Answers3

146

UPDATE

In Android Studio AVD:

  1. Open AVD Manager:

AVD menu

  1. Add/Edit AVD:

Specific AVD strip

  1. Click Advanced Settings in the bottom of the screen:

AVD Advanced Settings

  1. Set your camera of choice as the front/back cameras:

AVD Camera Settings

nurnachman
  • 4,367
  • 2
  • 33
  • 40
  • 4
    You should't set the front and back camera's to the same source. – Chris River Oct 14 '18 at 20:52
  • 1
    @ChrisRiver Why not? It shouldn't matter. – DaveTheMinion Jan 27 '19 at 18:36
  • 2
    Cool! But how to change webcam if we have more than one? – walkmn Feb 26 '19 at 09:54
  • 1
    If the Camera app crashes with the error "Can't connect to the camera", try rebooting the emulated device and clearing the Camera's app's cache & data. This worked for me. – Joseph238 May 09 '19 at 16:18
  • Sometimes it is helpful to set front and back camera's to same source, like when Camera isn't recognizing a webcam as a valid input source (i.e. when it only shows the other source and doesn't let you invert to the other camera) – Joseph238 May 09 '19 at 16:21
  • 5
    It instantly crashes simulator when the camera starts. I'm on a mac. – Milind Mevada May 12 '20 at 05:11
  • @MilindMevada, after a few days and a few laptop restarts, I noticed it works, and my face/ webcam feed is showing live on the emulator, sideways though. – Ben Butterworth May 25 '20 at 20:10
  • ok thanks. I had a permission issue that's why it was crashing. – Milind Mevada May 28 '20 at 08:42
  • 3
    @walkmn You can use alternate cam via the CLI: `cd "${ANDROID_HOME}/tools"; ./emulator -avd $VIRTUAL_DEVICE_AVD_ID -camera-back webcam1` – steamer25 Jun 21 '20 at 16:38
  • @MilindMevada make sure you allow the android emulator to use your mac hardware camera - if you didn't allow in the prompt on first try, you can set it in the privacy settings of your mac in system preferences. thanks for the highlight. – nurnachman Nov 12 '20 at 07:47
  • See also https://stackoverflow.com/questions/36832637/android-studios-emulator-has-an-error-cant-connect-to-camera/36904976 which fixed the follow-up error for me – user985366 Jan 02 '21 at 14:08
43

Follow the below steps in Eclipse.

  1. Goto -> AVD Manager
  2. Create/Edit the AVD.
  3. Hardware > New:
  4. Configures camera facing back
  5. Click on the property value and choose = "webcam0".
  6. Once done all the above the webcam should be connected. If it doesnt then you need to check your WebCam drivers.

Check here for more information : How to use web camera in android emulator to capture a live image?

enter image description here

Community
  • 1
  • 1
Lalith B
  • 11,413
  • 5
  • 28
  • 47
  • 2
    I think this is an older UI for the AVD manager. I used to have a window that looked like this, but now I can select the front and rear cameras from dedicated drop-down menus. – 1'' Jan 02 '13 at 16:01
  • I use a mac so eclipse looks this way in mac osx. So the windows and menus might be different in Microsoft windows and linux os. – Lalith B Jan 02 '13 at 17:41
  • Note that you also need to choose correct API. You can choose emulation for API 10 but it won't work. I think API 16 was the first to really support it. – Nux Jun 23 '13 at 01:54
  • No the emulation is based on ADT and not on Android API. ADT supports emulation since 19 I think. – Lalith B Jun 23 '13 at 14:26
  • In the Android Studio, for the step 3 select the "Show Advanced Settings" button to display the camera options. – eldes Sep 02 '16 at 13:15
3

I would suggest checking the drivers and updating them if required.

Tanuj Nayak
  • 607
  • 1
  • 8
  • 22
  • 2
    My driver, despite being 6 years old, is supposedly up-to-date and I can't find a newer version online. I may try changing cameras if I can find another. – 1'' Jan 01 '13 at 04:38