作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想制作一个只需要打开前置摄像头的应用程序,我该如何使用 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.
否则你也可以检查这些例子
希望对你有帮助
关于android - 如何在 android 中使用 Intent 打开前置摄像头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39423351/
我是一名优秀的程序员,十分优秀!