如何在android中使用Intent打开前置摄像头?

我想制作一个我必须只打开前置摄像头的应用程序,我该如何使用意图?
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getoutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
    intent.putExtra("android.intent.extras.CAMERA_FACING",1);

    // start the image capture Intent
    startActivityForResult(intent,CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

解决方法

java的
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri photoUri = Uri.fromFile(getoutputPhotoFile());
    intent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
    intent.putExtra("android.intent.extras.CAMERA_FACING",1);
    startActivityForResult(intent,CAMERA_PHOTO_REQUEST_CODE);

其他/替代解决方

private Camera openFrontFacingCameraGingerbread() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx,cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            try {
                cam = Camera.open(camIdx);
            } catch (RuntimeException e) {
                Log.e(TAG,"Camera Failed to open: " + e.toString());
            }
        }
    }

    return cam;
}

在AndroidManifest.xml文件添加这些权限

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

only available in Gingerbread(2.3) and Up Android Version.

否则你也可以查看这些例子

1. android-Camera2Basic

2. Camera Example 2

3. Vogella example

希望它可以帮助你..

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...