How to launch front camera with intent?

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

I'm using an intent to open the camera with the native application:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);      Uri photoUri = Uri.fromFile(getOutputPhotoFile());     intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);      startActivityForResult(intent, CAMERA_PHOTO_REQUEST_CODE); 

Every time the camera that is opened (front/back camera) is like the last time this native camera application was open. Meaning that if the last time I closed the native camera application the back camera was active, so when I launch the intent for camera, the back camera will be active.

I want to launch directly the front camera with the intent. Does anybody know how to do that?

回答1:

Word of caution: its a hack

Add this to the intent

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

This solution isn't sustainable, its using a testing code of the Camera app. For more info look at the "getCameraFacingIntentExtras" static method in Util class of the AOSP Camera project.

Update: Looks like that it was disabled in L



回答2:

Taken from Google Camera's shortcut for Android 7.1 (but should work with older Androids)

intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true); 

So, combined with previous answers, this works for me on all phones I could've test it on

intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT); intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1); intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true); 


回答3:

There's no intent (AFAIK) that specifically targets the front-facing camera.

To do it programmatically: Android SDK

Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int camNo = 0; camNo 

This example is only skeletal and doesn't provide any (much needed) error handling.

EDIT: You also need to add these to your manifest:



回答4:

Have you tried watching adb logcat while switching to the front camera in your native camera application? If it is indeed another activity, then it will show up as such there and you can simply copy the intent to your application. If it does not show up, you are out of luck, I guess.



回答5:

Try this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {   intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1); } else {   intent.putExtra("android.intent.extras.CAMERA_FACING", 1); } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!