Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9189735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:18:09+00:00 2026-06-17T20:18:09+00:00

I got Activity that call another class that have a Camera Preview. The problem

  • 0

I got Activity that call another class that have a Camera Preview.
The problem is that it’s open the back camera and i need the front.
how can i do that in default it will open the front camera(i looked in google but every thing i tried the app crashed when it opened).
Here is the Activity:

package com.elichai.tfillin;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.FrameLayout;

public class CameraActivity extends Activity {

   private Camera mCamera;
   private CameraPreview mPreview;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       mCamera = getCameraInstance();

       mPreview = new CameraPreview(this, mCamera);
       FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
       preview.addView(mPreview);
   }
   public static Camera getCameraInstance(){
       Camera c = null;
       try {
           c = Camera.open(); 
       }
       catch (Exception e){
       }
       return c; 
   }

}
Here Is the other Class:

package com.elichai.tfillin;

import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    int cameraId=0;

   @SuppressWarnings("deprecation")
   public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
       mCamera.release();
    }

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    mCamera.setDisplayOrientation(90);
   if (mHolder.getSurface() == null){
      return;
    }

    try {
        mCamera.stopPreview();
    } catch (Exception e){
    }

    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e){
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:18:10+00:00Added an answer on June 17, 2026 at 8:18 pm

    Your current Camera.open() has no parameter in it, meaning that it will open the default camera which is almost always the rear facing one.

    You should iterate through the available cameras and find out the ID of the front facing one and use that to open it. Something like:

    private Camera openFrontFacingCamera() 
    {
        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.getLocalizedMessage());
                }
            }
        }
    
        return cam;
    }
    

    And then use it in your app as follows:

    public static Camera getCameraInstance() {
       Camera c = null;
       try {
           c = openFrontFacingCamera();
       }
       catch (Exception e){
       }
       return c; 
    }
    

    Seeing as all both the methods do is open and return the Camera instance, you could easily simplify your code by directly calling openFrontFacingCamera() instead of getCameraInstance().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a BaseActivity class that extends Activity, in it I have @Override public
I got an Activity class that uses two other classes; one of them extending
I just got done writing an Android Activity that allows other Activities to call
I got an activity and a service that are binded. When the onServiceConnected is
I've got Activity A which fires up the Camera intent via: Intent intent =
I've got some Asynchronous cleanup activity I need to do as my Qt application
In the onCreate() of an activity, I've got a call to requestFocus() on an
I'm trying to launch an activity , when i got an incoming call for
I've got some Workflow Foundation 4 ActivityDesigners that I'd like to have interact with
I wrote a widget that can call numbers directly with one click. It works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.