Android Front Camera does not open

859 views Asked by At

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)
3

There are 3 answers

4
DDsix On

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.

1
CodeWalker On

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

0
Anil On

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.