Thursday, May 23, 2019

Camera Permission Programatically

Android newer version need permissions on run time so late's learn one of the Camera permission how we can implement on run time in android pro grammatically.

First you need to add permission in Android Manifest file



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

Then you have to add below code where you want to give permission for Camera.


ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA},

1);

Then the below code you have to put.
@Overridepublic void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {

            // If request is cancelled, the result are empty.            
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted,  Do the code
               
            } else {

                // permission denied, 
                Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
            }
            return;
        }

    }
}

You have done the code for Camera permission.

No comments:

Post a Comment