Comprehensive Guide to Camera Permission Handling in Android Marshmallow and Beyond

Nov 26, 2025 · Programming · 11 views · 7.8

Keywords: Android Permission Management | Runtime Permissions | Camera Permission Request

Abstract: This technical article provides an in-depth analysis of the runtime permission model introduced in Android 6.0 Marshmallow, with specific focus on camera permission implementation. It covers the complete workflow from permission declaration to runtime request handling, including permission checking, user dialog presentation, and result processing. The article integrates official Android development guidelines to present best practices for permission management, user experience optimization, and graceful degradation strategies when permissions are denied.

Runtime Permission Model Overview

Android 6.0 Marshmallow introduced a significant shift in permission management by categorizing permissions into normal and dangerous types. The camera permission android.permission.CAMERA is classified as dangerous, requiring explicit user consent at runtime on devices running Android 6.0 and higher, in addition to the traditional declaration in AndroidManifest.xml.

Permission Status Verification

Before requesting camera access, applications must verify the current permission status using ContextCompat.checkSelfPermission():

if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) 
    == PackageManager.PERMISSION_DENIED) {
    // Permission not granted, proceed with request
}

This method returns either PackageManager.PERMISSION_GRANTED or PackageManager.PERMISSION_DENIED, indicating the current authorization state. Developers should perform this check every time before using camera functionality, as users can revoke granted permissions at any time through system settings.

Permission Request Implementation

When permission is not granted, applications should use ActivityCompat.requestPermissions() to present the permission request dialog:

private static final int CAMERA_PERMISSION_REQUEST_CODE = 100;

ActivityCompat.requestPermissions(activity, 
    new String[] { Manifest.permission.CAMERA }, 
    CAMERA_PERMISSION_REQUEST_CODE);

The system displays a standardized permission request dialog where users can choose to allow or deny the permission. The request code CAMERA_PERMISSION_REQUEST_CODE is used to identify this specific permission request in subsequent callbacks.

Permission Result Handling

After users respond to the permission request, the system invokes the onRequestPermissionsResult() callback method:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
    if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission granted, proceed with camera functionality
            openCamera();
        } else {
            // Permission denied, implement graceful degradation
            handlePermissionDenied();
        }
    }
}

When permission is denied, applications should provide appropriate user guidance explaining why camera access is needed and how to re-enable the functionality.

Best Practices for Permission Requests

According to Android official guidelines, permission requests should be made in context when users interact with features requiring those permissions, rather than requesting all permissions at application startup. If users repeatedly deny a permission request, the system remembers this preference and may not show the dialog again for subsequent requests. Therefore, developers must carefully consider the timing and necessity of permission requests.

For sensitive permissions like camera access, it's recommended to request permission only when users explicitly trigger camera-related functionality (e.g., clicking a capture button), which improves grant rates while adhering to the principle of least privilege.

Compatibility Considerations

On devices running Android 5.1 and lower, camera permissions are automatically granted during installation, requiring only declaration in AndroidManifest.xml. To ensure code compatibility across API levels, it's advisable to use methods from the AndroidX compatibility library, such as ContextCompat.checkSelfPermission() and ActivityCompat.requestPermissions(), which function correctly across different Android versions.

User Guidance Strategies

When permissions are denied, applications should explain why camera access is necessary. The ActivityCompat.shouldShowRequestPermissionRationale() method can determine whether to display explanatory information:

if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
        Manifest.permission.CAMERA)) {
    // Show UI explaining why camera permission is needed
    showPermissionRationale();
} else {
    // Directly request permission
    requestCameraPermission();
}

This approach helps users understand permission necessities and improves the success rate of permission grants.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.