camera permission in android with code examples

In the age of social media and selfies, the camera is a crucial feature in any mobile device, including Android. Since the introduction of Android Marshmallow (API level 23), runtime permission has become mandatory. This means that app developers cannot access certain features (such as camera, location, contacts, etc.) of an Android device without the user's explicit permission.

In this article, we will guide you on how to request camera permission in your Android application using code examples.

Step 1: Add Permissions to Android Manifest

The first step is to define the permissions in the AndroidManifest.xml file. This file is an essential element of any Android application and lists all the features that the app requires access to. In the case of camera permission, we need to add the following lines of code in the manifest file:

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

This code informs the Android operating system that your application requires camera access permission. By including this permission in the AndroidManifest.xml file, the user will be able to see that your app wants to access the camera feature.

Step 2: Request Camera Permission

The second step is to request camera permission from the user when the app launches. We'll use the following code example to ask the user to grant permission:

private final int REQUEST_CAMERA_PERMISSION = 1;

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.CAMERA }, REQUEST_CAMERA_PERMISSION);

There are a few things to note about this code example. Firstly, we are using the ContextCompat.checkSelfPermission method to check if the user has already granted permission to the camera feature. Secondly, the if condition checks if the permission has not been granted. If the permission hasn't been granted, we request permission through the ActivityCompat.requestPermissions method.

The final parameter of this method is an integer code that identifies our request. This request code is a unique identifier that we use to distinguish between different permission requests within our application.

Step 3: Handle Request Permissions Result

The final step is to handle permission requests result in the onRequestPermissionsResult method. This method is triggered when the user grants or denies a permission request initiated by the application. We'll use the following code example:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch(requestCode) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this, "Camera permission granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "Camera permission denied", Toast.LENGTH_SHORT).show();

The onRequestPermissionsResult method receives three parameters: requestCode, permissions, and grantResults. The requestCode is the unique identifier that we set earlier. The permissions array contains the requested permissions, and the grantResults integer array contains the user's response to each permission request.

In the above code example, we have used a switch case to handle the request code for camera permission. If the user grants permission, we display a Toast message, indicating that camera permission has been granted. However, if the user denies permission, we show a different Toast message.


In conclusion, we have provided you with an easy-to-follow guide for requesting camera permission in Android applications. We hope that this guide has been helpful and has provided you with the necessary information to integrate camera permission functionality in your application. Remember always to request user permission and handle user responses accordingly, as this contributes to a more secure and trustworthy application.

I can provide you with more information about the previous topics.

Android Marshmallow (API level 23) introduced a significant change to how app permissions are managed in Android devices. Before Marshmallow, apps could request permission to access a feature during installation, and users could only grant or deny permission for the entire app. This was problematic because users were often not aware of the permissions they were granting, and apps could access sensitive features without the user's consent.

With Marshmallow, runtime permissions were introduced, which allows the user to grant specific permissions to an app when they are needed, rather than during installation. This gives users more control over their privacy while still allowing apps to use the features they need.

Runtime permissions are requested by the app via a system dialog, making it clear to the user which permissions are being requested by the app. The user can then choose to grant or deny the requested permission(s). If the user denies permission, the app will not be able to access the feature until the user grants permission in the app settings.

In the case of camera permission, it's important to note that not all devices have a camera. Therefore, you should always check if the device has a camera before requesting permission. You can use the following code examples to check for a camera:

// Check if the device has a camera
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    // The device has a camera
} else {
    // The device does not have a camera

There are different levels of permission protection for different types of permissions.

Normal permissions are granted to an app automatically, without the user's explicit consent. These are typically low-risk permissions, such as access to the Internet or access to the user's contact list.

Dangerous permissions, on the other hand, can grant the app access to sensitive user data or features, such as camera and location. These permissions require the user's explicit consent, and a system dialog is displayed when the app requests such permissions.

In conclusion, runtime permissions are a crucial aspect of Android app development. They provide users with more control over their privacy and ensure that apps can access sensitive features only with the user's explicit consent. By following the steps outlined in this article, you can ensure that your app requests camera permission correctly and help build user trust in your application.

Popular questions

Sure, here are 5 questions along with their answers:

  1. What is the purpose of requesting camera permission in Android?
    Answer: The purpose of requesting camera permission in Android is to access the camera feature of the device and allow the app to use it for taking pictures, video recording, or other functionalities.

  2. When did runtime permissions become mandatory in Android, and why?
    Answer: Runtime permissions became mandatory in Android with the introduction of Android Marshmallow (API level 23). This was done to give users more control over their privacy by allowing them to grant or deny specific permissions to apps on the fly.

  3. What are the three steps to request camera permission in an Android application?
    Answer: The three steps to request camera permission in an Android application are as follows:

  • Add Camera Permission to Android Manifest
  • Request Camera Permission
  • Handle Request Permissions Result
  1. What happens if the user denies camera permission to an app?
    Answer: If the user denies camera permission to an app, the app will not be able to access the camera functionality until the user grants permission in the app settings.

  2. How can you check if a device has a camera in Android?
    Answer: You can check if a device has a camera in Android by using the following code example:

if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    // The device has a camera
} else {
    // The device does not have a camera

I hope this helps!



As an experienced software engineer, I have a strong background in the financial services industry. Throughout my career, I have honed my skills in a variety of areas, including public speaking, HTML, JavaScript, leadership, and React.js. My passion for software engineering stems from a desire to create innovative solutions that make a positive impact on the world. I hold a Bachelor of Technology in IT from Sri Ramakrishna Engineering College, which has provided me with a solid foundation in software engineering principles and practices. I am constantly seeking to expand my knowledge and stay up-to-date with the latest technologies in the field. In addition to my technical skills, I am a skilled public speaker and have a talent for presenting complex ideas in a clear and engaging manner. I believe that effective communication is essential to successful software engineering, and I strive to maintain open lines of communication with my team and clients.
Posts created 2138

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top