1

I am trying to take a photo in my Android application using my PC's built-in webcam. I am using the eclipse Android emulator and have set the AVD to use webcam0 as the rear-facing camera, but when I run my program it always crashes, saying "Unfortunately, Camera has stopped". I have added the following line to my Manifest xml:

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

though it still does not seem to work. I have read in a few places that there's supposed to be a "Hardware" section in the AVD manager edit/create screen, but mine does not have it.

Am I missing something? Here is the logcat that appears when I try to run the app:

Any thoughts about what might be happening? I've searched for solutions all over the place and can't seem to find any that solve this exact problem. Thanks for the help.

EDIT Here's my image capture button/method code:

/* Create capture button */
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    //create directory/picture file
    count++;
    file = dir + count + ".jpg";
    File picFile = new File(file);
    try {
        picFile.createNewFile();
        } catch (IOException e) {}       
    Uri outputFileUri = Uri.fromFile(picFile);
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});

/* Check if valid photo */
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
    Log.d("CameraDemo", "Pic saved");
}
}

This code was mostly created based on a thread I found here on SO, I'm afraid I don't recall which one though.

DerStrom8
  • 1,311
  • 2
  • 23
  • 45
  • Take a look here. Similar Error: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails – user2511882 Dec 01 '13 at 20:58
  • Have you tried running your code on some phone? Is it crashing? Also, please post your code, there might be a bug there... – Melquiades Dec 01 '13 at 21:40
  • @Melquiades I have not yet run this on an actual phone. It's still in the development phase, and I'll need to tie up some loose ends before loading it onto my Android device. I have added in the code I use for the camera. user2511882, it seems that the thread you linked uses the camera in a much different way. I'm not sure if it simply can't be done the way I'm doing it, or if there is a way to make mine work without adding new classes. – DerStrom8 Dec 01 '13 at 22:05
  • "though it still does not seem to work" - what do you mean? is it force-closing at the start? on button press? Can you please post full code so I can check on my emulator? – Melquiades Dec 01 '13 at 22:10
  • I have that method called on a button press. The button is called "capture", and when the user clicks it it is supposed to run the camera capture process that's pre-programmed into the device. I really can't post all the code, it's very extensive and contains a lot of unrelated and personal data. However, if you create a button that calls the method I showed above that's called by the onClickListener, you should be able to emulate it without a problem. – DerStrom8 Dec 02 '13 at 16:10

2 Answers2

0

Have you added:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

in your manifest?

Melquiades
  • 8,496
  • 1
  • 31
  • 46
  • Yes, I added that to the manifest a while back when I first created the method to take a photo. Anyway, that wouldn't cause it to crash before the photo is even taken, would it? – DerStrom8 Dec 02 '13 at 16:07
0

If you feel that you have followed all the right procedures for activating your camera but fails, then as for my case, I had to try the in-built camera in the emulator to rule out any possibility that it is my codes. I realized that the same error was being produced by the default camera app - as you reported.

So, after several trials: (adding space, ram, more manifest files, clearing Google Play Services Data, updating Play Services, etc), I decided to use an Android Studio on Windows 10 x64 machine (the earlier one was Windows 7 x64). To my surprise, the error was gone and the camera did not crash. Now, I don't know if it has to do with OS (Win 7) or the Processor, or any other hardware or software issue. You can try this if you are not already using Windows 10.

Ajowi
  • 449
  • 3
  • 12