Runtime camera permission
Hi,Today I am going to explain you how to set runtime camera permission in android. Android is a mobile operating system,where each application in android is separated from another through its distinct id and data is private to that application only. Each Android application starts in its own process thus they are different from all other applications.
An android app can’t access any file or data outside its scope until and unless the file or data is shared with the application.If an application needs anything outside its scope, then it should request for permission to the user.Every application can request required permissions. For example, an application may declare that it requires camera access.Basically there are two type of permission one is normal permission and another is dangerous permissions.
Normal permission
Normal permissions do not directly affect the user’s privacy.We set these permission inside the manifest.xml.
Examples are given below.
<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE”/>
Dangerous permission
Here is the list of dangerous permission.
<uses-permission android:name=”android.permission.CAMERA”/>
<uses-permission android:name=”android.permission.CALL_PHONE”/>
So I am going to show you how to set run time camera permission.
Request for Permission
/*Camera permission*/ if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED){ ActivityCompat.requestPermissions(ActivityHomeFragment.this, new String[] {Manifest.permission.CAMERA}, 100); }
Handle the Permissions Request Response
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 100) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show(); } } }
See our android other tutorial for showing dialogue box while exit from application.
Exit application using permission