前置摄像头未在Android平台中打开 [英] Front camera is not opening in android platform

查看:141
本文介绍了前置摄像头未在Android平台中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在应用程序中单击捕获照片"图标时,默认情况下它将打开前置摄像头,但是每次打开后置摄像头.我需要手动更改它才能打开前置摄像头.

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 camera.

     So can anyone please help me out on the above issue.

下面是我打开前置摄像头模式的代码.

Below is my code to open front camera mode .

    private void getPhotoFromCamera() 
    {
        try 
        {
            PackageManager pm = mContext.getPackageManager();
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
           intent.putExtra("android.intent.extras.CAMERA_FACING",1);
            VisitorRegistrationFragment.this.startActivityForResult(intent,FROM_CAMERA);
         } 
         catch (Exception e) 
         {
            e.printStackTrace();
         } 
         catch (OutOfMemoryError e) 
         {
            e.printStackTrace();
         }

    }

推荐答案

这是打开前置摄像头的代码 我的javafile:

This is code to open front camera My javafile :

public class MainActivity extends Activity {
    int TAKE_PHOTO_CODE = 0;
    public static int count = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(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.
        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
        File newdir = new File(dir);
        newdir.mkdirs();

        Button capture = (Button) findViewById(R.id.button);
        capture.setOnClickListener(new View.OnClickListener() {
            public void onClick(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++;
                String file = dir + count + ".jpg";
                File newfile = new File(file);
                   try {
                        newfile.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                Uri outputFileUri = Uri.fromFile(newfile);

                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra("android.intent.extras.CAMERA_FACING",1);
               // cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

                startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
            Log.d("CameraDemo", "Pic saved");
        }
    }
}

我的Xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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">


    <Button
        android: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>

需要两个许可:-

 <uses-permission android:name="android.permission.CAMERA"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

这篇关于前置摄像头未在Android平台中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆