How To Create A Camera Application In Android Using Android Studio

Introduction

 
Android is one of the most popular operating systems for mobiles. In this article, I will show you how to start the camera application in Android using Android Studio.
 
Requirements
Steps to be followed
 
Follow these steps to create an application that starts the camera in Android. I have included the source code below.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
Android
 
Step 2
 
You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
 
Android
 
Now, select the version of Android and select the target Android devices.
 
Android
 
Step 3
 
Now, add the activity and click the "Next" button.
 
Android
 
Add activity name and click "Finish".
 
Android
 
Step 4
 
Go to activity_main.xml, This XML file contains the designing code for your Android app.
 
Android
 
The XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     tools:context="abu.camera.MainActivity">  
  9.   
  10.     <RelativeLayout  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:orientation="vertical">  
  14.     <Button  
  15.             android:id="@+id/button1"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_alignParentLeft="true"  
  19.             android:layout_alignParentTop="true"  
  20.             android:layout_marginLeft="81dp"  
  21.             android:layout_marginTop="54dp"  
  22.             android:text="Button" />  
  23.     </RelativeLayout>  
  24. </android.support.constraint.ConstraintLayout>  
Step 5
 
Go to  Main Activity.java. This Java program is the backend language for the Android app.
 
Android
 
The Java code is given below.
  1. package abu.camera;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.provider.MediaStore;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     Button btn;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         btn=(Button)findViewById(R.id.button1);  
  19.         btn.setOnClickListener(new OnClickListener() {  
  20.   
  21.             public void onClick(View v) {  
  22.                 // TODO Auto-generated method stub  
  23.                 Intent i=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);  
  24.                 startActivity(i);  
  25.             }  
  26.         });  
  27.     }  
  28.     @Override  
  29.     public boolean onCreateOptionsMenu(Menu menu) {  
  30.         // Inflate the menu; this adds items to the action bar if it is present.  
  31.         getMenuInflater().inflate(R.menu.main, menu);  
  32.         return true;  
  33.     }  
  34. }  
Step 6
 
Now, go to the menu bar and click "Make Project" or press ctrl+f9 to debug the errors.
 
Android
 
Step 7
 
Then, click the "Run" button or press shift+f10 to run the project. Choose the "virtual machine" option and click OK.
 
Android
 

Conclusion

 
We have successfully created an app that starts the camera in Android.
 
Android
 
Android
 
Android


Similar Articles