0

i have tried to implement the concept to capture image via front camera.So, i have followed the step of the below site http://www.vogella.com/tutorials/AndroidCamera/article.html but i have encounter the issue while click the button. Can you some one help me on this please.

ImagePickActivity

   public class ImagePickActivity extends Activity {

    private Camera camera;
    private int cameraId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_pick);

        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("Camera", "Camera found");
                cameraId = i;
                break;
              }
            }
            return cameraId;
          }



      @Override
      protected void onPause() {
        if (camera != null) {
          camera.release();
          camera = null;
        }
        super.onPause();
      }
 }




public class PhotoHandler implements PictureCallback {

      private final Context context;

      public PhotoHandler(Context context) {
        this.context = context;
      }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        // TODO Auto-generated method stub

        File pictureFileDir = getDir();

        if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

          Log.d("Camara", "Can't create directory to save image.");
          Toast.makeText(context, "Can't create directory to save image.",
              Toast.LENGTH_LONG).show();
          return;
     }


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String date = dateFormat.format(new Date());
        String photoFile = "Picture_" + date + ".jpg";

        String filename = pictureFileDir.getPath() + File.separator + photoFile;

        File pictureFile = new File(filename);

        try {
          FileOutputStream fos = new FileOutputStream(pictureFile);
          fos.write(data);
          fos.close();
          Toast.makeText(context, "New Image saved:" + photoFile,
              Toast.LENGTH_LONG).show();
        } catch (Exception error) {
          Log.d("Camara", "File" + filename + "not saved: "
              + error.getMessage());
          Toast.makeText(context, "Image could not be saved.",
              Toast.LENGTH_LONG).show();
        }
      }

      private File getDir() {
            File sdDir = Environment
              .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            return new File(sdDir, "CameraAPIDemo");
          }
     } 

LogCat

FATAL EXCEPTION: main Process: com.frontcamera, 
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3848)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:82)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3843)
Caused by: java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1597)
at android.hardware.Camera.takePicture(Camera.java:1542)
at com.frontcamera.ImagePickActivity.onClick(ImagePickActivity.java:44)
Ravi Vaghela
  • 3,420
  • 2
  • 23
  • 51
Vengat
  • 235
  • 1
  • 5
  • 16

3 Answers3

0

You have to show the camera preview before you call takePicture. If you wish to takePicture without displaying the preview, you can make this trick: show a preview, just not on the screen. Search the forum for this, as it has been discussed on stackoverflow before.

DDsix
  • 1,966
  • 1
  • 18
  • 23
  • I have used the below intent, Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); but it's open the back camera. If there is any intent available to open the front camera. – Vengat Feb 24 '16 at 11:48
  • Let me clear it up: I didn't say open the default camera app via Intent. I said that if you want to successfully call takePicture on your camera object, you have to display a preview of the camera. Read here about this: http://developer.android.com/training/camera/cameradirect.html – DDsix Feb 24 '16 at 11:54
  • It clearly says in the documentation "Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture." – DDsix Feb 24 '16 at 11:55
  • Read this too: http://developer.android.com/reference/android/hardware/Camera.html You shouldn't go and read random tutorials on the Internet unless you have read the Google official documentation first. – DDsix Feb 24 '16 at 11:56
0

Add permissions to your manifest.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />


Also take a look here - java.lang.RuntimeException: takePicture failed

Community
  • 1
  • 1
CodeWalker
  • 2,281
  • 4
  • 23
  • 50
  • I wouldn't recommend adding unless your app's main functionality is taking pictures -> you can build you app around that. If your app does some other stuff too, then you're preventing users who use devices without a camera to install it and use your app. In this case, it's almost ok, since pretty much all devices have cameras. But this goes in general. – DDsix Feb 24 '16 at 11:15
0

I think you are mixing implicit and explicit intents.

The code from vogella is doing that: Displaying a camera preview and taking the picture when the button is clicked without closing your application.

The intent you are using: Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); This is implicit intent, you let the user take a picture with an app of his/her choice (Instagram-Native camera etc.). This is going out of your application, and returning when the photo is taken with the photo data.

You should first decide to do one of these things.

Anil
  • 543
  • 3
  • 16