How to launch front camera with intent?

后端 未结 6 1677
[愿得一人]
[愿得一人] 2020-11-27 18:47

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

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    Uri photoUri = Uri         


        
相关标签:
6条回答
  • 2020-11-27 19:17

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

    To do it programmatically: Android SDK <= 2.2 only supports use of a single camera (the first back-facing camera). For 2.3+, you can loop thru the cameras and figure out which is the front facing one (if available). It'd be something like...

    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
        CameraInfo camInfo = new CameraInfo();
        Camera.getCameraInfo(camNo, camInfo);
        if (camInfo.facing.equals(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
            cam = Camera.open(camNo);
        }
    }
    if (cam == null) {
       // no front-facing camera, use the first back-facing camera instead.
       // you may instead wish to inform the user of an error here...
       cam = Camera.open();
    }
    // ... do stuff with Camera cam ...
    

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

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

    <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" />
    
    0 讨论(0)
  • 2020-11-27 19:19
        pictureIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
        pictureIntent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
        pictureIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
    
    working on intex 
    
    0 讨论(0)
  • 2020-11-27 19:24

    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.

    0 讨论(0)
  • 2020-11-27 19:32

    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

    0 讨论(0)
  • 2020-11-27 19:38

    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);
    
    0 讨论(0)
  • 2020-11-27 19:42

    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);
    }
    
    0 讨论(0)
提交回复
热议问题