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. <RelativeLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:orientation="vertical">
  13. <Button
  14. android:id="@+id/button1"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentLeft="true"
  18. android:layout_alignParentTop="true"
  19. android:layout_marginLeft="81dp"
  20. android:layout_marginTop="54dp"
  21. android:text="Button" />
  22. </RelativeLayout>
  23. </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. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.provider.MediaStore;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. public class MainActivity extends Activity {
  11. Button btn;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. btn=(Button)findViewById(R.id.button1);
  17. btn.setOnClickListener(new OnClickListener() {
  18. public void onClick(View v) {
  19. // TODO Auto-generated method stub
  20. Intent i=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
  21. startActivity(i);
  22. }
  23. });
  24. }
  25. @Override
  26. public boolean onCreateOptionsMenu(Menu menu) {
  27. // Inflate the menu; this adds items to the action bar if it is present.
  28. getMenuInflater().inflate(R.menu.main, menu);
  29. return true;
  30. }
  31. }
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