Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to open Camera using intent on button click.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.47k
    Comment on it

    Here we will learn how to open camera in android, to open Camera in the android app we have to add

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


    permission in our manifest file.

    In this example I'm moving to Camera through button click event so I have taken button in my xml file and on its click event I have opened Camera using Intent .

    Now copy the code written below in your MainActivity.java  file of your project.
     

    MainActivity.java

    
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    
        Button btn=(ImageButton)findViewById(R.id.Take_Image_btn);
        btn.setOnClickListener(mClickListener);
    }
    
    View.OnClickListener mClickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this,"ENtered in On ClickListener",Toast.LENGTH_LONG).show();
                Intent mintent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                 startActivity(mintent);
      }
        };

     

    activity_main.xml

    <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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".LoginActivity">
    
    
        <Button
            android:layout_centerHorizontal="true"
            android:id="@+id/Take_Image_btn"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:background="@drawable/button"/>
    
    </RelativeLayout>
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: