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.
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 "Next" button.
Now, select the version of Android and select the target Android devices.
Step 3
Now, add the activity and click "Next" button.
Add activity name and click "Finish".
Step 4
Go to activity_main.xml, This XML file contains the designing code for your Android app.
The XML code is given below.
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="abu.camera.MainActivity">
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:layout_marginLeft="81dp"
- android:layout_marginTop="54dp"
- android:text="Button" />
- </RelativeLayout>
- </android.support.constraint.ConstraintLayout>
Step 5
Go to Main Activity.java. This Java program is the backend language for Android app.
The Java code is given below.
- package abu.camera;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class MainActivity extends Activity {
- Button btn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn=(Button)findViewById(R.id.button1);
- btn.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
-
- Intent i=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
- startActivity(i);
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Step 6
Now, go to menu bar and click "Make Project" or press ctrl+f9 to debug the errors.
Step 7
Then, click "Run" button or press shift+f10 to run the project. Choose the "virtual machine" option and click OK.
Conclusion
We have successfully created an app that starts the camera in Android.