Front Camera Is Not Opening In Android Platform
When I click on capture photo icon in my application, by default it should open front camera but every time it is opening back camera. I need to change it manually to open front c
Solution 1:
Change to use front camera
intent.putExtra("android.intent.extras.CAMERA_FACING",0);
Hardcoding this values is a bad idea and should better use
intent.putExtra("android.intent.extras.CAMERA_FACING", Camera.CameraInfo.CAMERA_FACING_FRONT)
Solution 2:
This is code to open front camera My javafile :
publicclassMainActivityextendsActivity {
intTAKE_PHOTO_CODE=0;
publicstaticintcount=0;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Here, we are making a folder named picFolder to store// pics taken by the camera using this application.finalStringdir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
Filenewdir=newFile(dir);
newdir.mkdirs();
Buttoncapture= (Button) findViewById(R.id.button);
capture.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View v) {
// Here, the counter will be incremented each time, and the// picture taken by camera will be stored as 1.jpg,2.jpg// and likewise.
count++;
Stringfile= dir + count + ".jpg";
Filenewfile=newFile(file);
try {
newfile.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
UrioutputFileUri= Uri.fromFile(newfile);
IntentcameraIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("android.intent.extras.CAMERA_FACING",1);
// cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
}
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
}
}
}
my Xml File:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="aavid.rks.stackovewrflow.MainActivity"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Click pic Using Front camera"android:id="@+id/button"android:layout_alignParentTop="true"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_marginLeft="93dp"android:layout_marginStart="93dp"android:layout_marginTop="133dp" /></RelativeLayout>
two permission needed :-
<uses-permissionandroid:name="android.permission.CAMERA"/><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Solution 3:
private Camera openFrontFacingCameraGingerbread() {
intcameraCount=0;
Cameracam=null;
Camera.CameraInfocameraInfo=newCamera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (intcamIdx=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;
}
Add the following permissions in the AndroidManifest.xml file:
<uses-permissionandroid:name="android.permission.CAMERA" /><uses-featureandroid:name="android.hardware.camera"android:required="false" /><uses-featureandroid:name="android.hardware.camera.front"android:required="false" />
Solution 4:
I think you must add this in your onCreate() method :
@Overridepublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (currentCameraId ==Camera.CameraInfo.CAMERA_FACING_BACK) {
currentCameraId =Camera.CameraInfo.CAMERA_FACING_FRONT;
} else {
currentCameraId =Camera.CameraInfo.CAMERA_FACING_BACK;
}
camera =Camera.open(currentCameraId);
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
It worked for me. Hope so it might be helpful for you too.
Post a Comment for "Front Camera Is Not Opening In Android Platform"