1

I am using this tutorial:

http://www.vogella.com/articles/AndroidCamera/article.html

To capture an image and save it on SD Card, but whenever i run this program, getting : Unfortunately App has Stopped

Error says:

Caused by: java.lang.reflect.InvocationTargetException

Line Number 39 is:

camera.takePicture(null, null, new PhotoHandler(getApplicationContext()));

Logcat output:

05-29 16:19:38.351: E/AndroidRuntime(767): FATAL EXCEPTION: main
05-29 16:19:38.351: E/AndroidRuntime(767): java.lang.IllegalStateException: Could not execute method of the activity
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View$1.onClick(View.java:3599)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View.performClick(View.java:4204)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View$PerformClick.run(View.java:17355)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.os.Handler.handleCallback(Handler.java:725)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.os.Looper.loop(Looper.java:137)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-29 16:19:38.351: E/AndroidRuntime(767):  at dalvik.system.NativeStart.main(Native Method)
05-29 16:19:38.351: E/AndroidRuntime(767): Caused by: java.lang.reflect.InvocationTargetException
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 16:19:38.351: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 16:19:38.351: E/AndroidRuntime(767):  at android.view.View$1.onClick(View.java:3594)
05-29 16:19:38.351: E/AndroidRuntime(767):  ... 11 more
05-29 16:19:38.351: E/AndroidRuntime(767): Caused by: java.lang.NullPointerException
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.example.cameraapp.MakePhotoActivity.onClick(MakePhotoActivity.java:39)
05-29 16:19:38.351: E/AndroidRuntime(767):  ... 14 more

MakePhotoActivity.java:

  public class MakePhotoActivity extends Activity {
  final static String DEBUG_TAG = "MakePhotoActivity";
  private Camera camera;
  private int cameraId = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // do we have a camera?
    if (!getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
      Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
          .show();
    } else {
      cameraId = findFrontFacingCamera();
      if (cameraId < 0) {
        Toast.makeText(this, "No front facing camera found.",
            Toast.LENGTH_LONG).show();
      } else {
        camera = Camera.open(cameraId);
      }
    }
  }

  public void onClick(View view) {
    camera.takePicture(null, null,
        new PhotoHandler(getApplicationContext()));
  }

  private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
        Log.d(DEBUG_TAG, "Camera found");
        cameraId = i;
        break;
      }
    }
    return cameraId;
  }

  @Override
  protected void onPause() {
    if (camera != null) {
      camera.release();
      camera = null;
    }
    super.onPause();
  }
Chulbul Pandey
  • 506
  • 1
  • 8
  • 20

4 Answers4

5

To Open Android default Camera you write a simple 2 lines of code which are:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);

and to get the path of the clicked image you need to Override onActivityResult(). The following code helps you get the path of the clicked image:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == Activity.RESULT_OK && requestCode == 0) {
    String result = data.toURI();
    // ...
  }
}

Finally, in your manifest file, add a "Uses-Permission" for the camera.

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

: Updated Answer :

For Custom Camra

Follow this links for Custom Camera:

  1. Custom Camera Application
  2. Custom camera for Android created for testing, playing and learning
  3. How to capture and save an image using custom camera in Android?
  4. How to create a custom layout for your camera in Android?
Community
  • 1
  • 1
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
  • 2)Custom camera for Android created for testing, playing and learning This example helps me to solve this issue. I refer CameraPreview class surfaceChanged method – Vikram Dec 19 '13 at 09:32
0

I think your problem is here :

Caused by: java.lang.NullPointerException
05-29 16:19:38.351: E/AndroidRuntime(767):  at com.example.cameraapp.MakePhotoActivity.onClick(MakePhotoActivity.java:39)
05-29 16:19:38.351: E/AndroidRuntime(767):  ... 14 more

Check what is wrong @ MakePhotoActivity.java, line 39.

Vittorio Cozzolino
  • 931
  • 1
  • 14
  • 31
0

Seems that your camera object is null

Eugene Popovich
  • 3,343
  • 2
  • 30
  • 33
-3

You should need the camera on your AVD So edit your AVD and add camera on back as well as on front.....