我想制作一个我必须只打开前置摄像头的应用程序,我该如何使用意图?
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; }
<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.
否则你也可以查看这些例子
希望它可以帮助你..