Android Front Camera无法打开

时间:2016-02-24 10:33:35

标签: android camera front-camera

我试图通过前置摄像头实现捕获图像的概念。所以,我已经按照下面的网站http://www.vogella.com/tutorials/AndroidCamera/article.html的步骤,但我点击按钮时遇到了问题。你可以请一位帮助我的人。

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 个答案:

答案 0 :(得分:0)

在调用takePicture之前,您必须先显示相机预览。如果您希望在不显示预览的情况下拍摄照片,您可以制作此技巧:显示预览,而不是在屏幕上显示。在论坛中搜索此内容,因为之前已在stackoverflow上讨论过。

答案 1 :(得分:0)

为您的清单添加权限。

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


另请看这里 - java.lang.RuntimeException: takePicture failed

答案 2 :(得分:0)

我认为你混合了隐含和显式意图。

vogella的代码正在这样做: 在不关闭应用程序的情况下单击按钮时显示摄像机预览并拍摄照片。

您正在使用的意图: Intent intent = new Intent(&#34; android.media.action.IMAGE_CAPTURE&#34;); 这是隐含意图,您让用户使用他/她选择的应用程序(Instagram-Native相机等)拍照。这不在您的应用程序中,并在照片与照片数据一起拍摄时返回。

您应该首先决定做其中一件事。