How To Get Camera Permission In Android
Starting from Android six.0 (API 23), users are non asked for permissions at the time of installation rather developers need to request the permissions at the run fourth dimension. Only the permissions that are defined in the manifest file tin can be requested at run time.
Types of Permissions
i. Install-Time Permissions: If the Android five.1.1 (API 22) or lower, the permission is requested at the installation time at the Google Play Shop.
If the user Accepts the permissions, the app is installed. Else the app installation is canceled.
2. Run-Time Permissions: If the Android six (API 23) or higher, the permission is requested at the run time during the running of the app.
If the user Accepts the permissions, and then that feature of the app can exist used. Else to use the feature, the app requests permission again.
So, now the permissions are requested at runtime. In this article, we will discuss how to request permissions in an Android Application at run fourth dimension.
Steps for Requesting permissions at run fourth dimension
Step 1: Declare the permission in the Android Manifest file : In Android, permissions are declared in the AndroidManifest.xml file using the uses-permission tag.
<uses-permission android:name="android.permission.PERMISSION_NAME"/>
Hither we are declaring storage and camera permission.
XML
< uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" />
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
< uses-permission android:name = "android.permission.CAMERA" />
Step 2: Modify activity_main.xml file to Add two buttons to request permission on button click: Permission will be checked and requested on button click. Open the activity_main.xml file and add 2 buttons to it.
XML
< Push button
android:id = "@+id/storage"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Storage"
android:layout_marginTop = "16dp"
android:padding = "8dp"
android:layout_below = "@id/toolbar"
android:layout_centerHorizontal = "truthful" />
< Button
android:id = "@+id/camera"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Camera"
android:layout_marginTop = "16dp"
android:padding = "8dp"
android:layout_below = "@id/storage"
android:layout_centerHorizontal = "true" />
Step three: Check whether permission is already granted or non. If permission isn't already granted, request the user for the permission: In order to use whatsoever service or characteristic, the permissions are required. Hence nosotros have to ensure that the permissions are given for that. If not, then the permissions are requested.
Bank check for permissions: Kickoff with Android 6.0 (API level 23), the user has the right to revoke permissions from whatever app at whatever time, even if the app targets a lower API level. So to apply the service, the app needs to check for permissions every time.
Syntax:
if(ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { // Permission is non granted } Asking Permissions: When PERMISSION_DENIED is returned from the checkSelfPermission() method in the to a higher place syntax, we need to prompt the user for that permission. Android provides several methods that tin can be used to request permission, such as requestPermissions().
Syntax:
ActivityCompat.requestPermissions(MainActivity.this, permissionArray, requestCode); Hither permissionArray is an assortment of type String.
Case:
Java
public void checkPermission(String permission, int requestCode)
{
if (ContextCompat.checkSelfPermission(MainActivity. this , permission) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(MainActivity. this , new Cord[] { permission }, requestCode);
}
else {
Toast.makeText(MainActivity. this , "Permission already granted" , Toast.LENGTH_SHORT).evidence();
}
}
Kotlin
private fun checkPermission(permission: Cord, requestCode: Int) {
if (ContextCompat.checkSelfPermission( this @MainActivity , permission) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions( this @MainActivity , arrayOf(permission), requestCode)
} else {
Toast.makeText( this @MainActivity , "Permission already granted" , Toast.LENGTH_SHORT).show()
}
}
This role will show a Toast bulletin if permission is already granted otherwise prompt the user for permission.
Step four: Override onRequestPermissionsResult() method: onRequestPermissionsResult() is chosen when user grant or decline the permission. RequestCode is ane of the parameters of this part which is used to check user action for the respective requests. Here a toast message is shown indicating the permission and user activeness.
Instance:
Java
@Override
public void onRequestPermissionsResult( int requestCode,
@NonNull Cord[] permissions,
@NonNull int [] grantResults)
{
super .onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_CODE) {
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();
}
}
else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity. this , "Storage Permission Granted" , Toast.LENGTH_SHORT).bear witness();
}
else {
Toast.makeText(MainActivity. this , "Storage Permission Denied" , Toast.LENGTH_SHORT).show();
}
}
}
Kotlin
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>,
grantResults: IntArray) {
super .onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText( this @MainActivity , "Photographic camera Permission Granted" , Toast.LENGTH_SHORT).show()
} else {
Toast.makeText( this @MainActivity , "Camera Permission Denied" , Toast.LENGTH_SHORT).show()
}
} else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText( this @MainActivity , "Storage Permission Granted" , Toast.LENGTH_SHORT).show()
} else {
Toast.makeText( this @MainActivity , "Storage Permission Denied" , Toast.LENGTH_SHORT).show()
}
}
}
Beneath is the complete code of this application:
Beneath is the lawmaking for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-viii" ?>
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< android.back up.v7.widget.Toolbar
android:id = "@+id/toolbar"
android:layout_width = "match_parent"
android:background = "@color/colorPrimary"
app:title = "GFG | Permission Example"
app:titleTextColor = "@android:color/white"
android:layout_height = "?android:attr/actionBarSize" />
< Button
android:id = "@+id/storage"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Storage"
android:layout_marginTop = "16dp"
android:padding = "8dp"
android:layout_below = "@id/toolbar"
android:layout_centerHorizontal = "truthful" />
< Button
android:id = "@+id/photographic camera"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Camera"
android:layout_marginTop = "16dp"
android:padding = "8dp"
android:layout_below = "@id/storage"
android:layout_centerHorizontal = "truthful" />
</ RelativeLayout >
Below is the code for the AndroidManifest.xml file.
XML
<? xml version = "one.0" encoding = "utf-8" ?>
parcel = "org.geeksforgeeks.requestPermission" >
< uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" />
< uses-permission android:proper name = "android.permission.WRITE_EXTERNAL_STORAGE" />
< uses-permission android:name = "android.permission.CAMERA" />
< application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:characterization = "@string/app_name"
android:roundIcon = "@mipmap/ic_launcher_round"
android:supportsRtl = "truthful"
android:theme = "@manner/AppTheme" >
< activity
android:proper noun = ".MainActivity" >
< intent-filter >
< activeness
android:name = "android.intent.action.MAIN" />
< category
android:proper noun = "android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
</ application >
</ manifest >
Below is the code for the MainActivity file.
Kotlin
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
companion object {
private const val CAMERA_PERMISSION_CODE = 100
private const val STORAGE_PERMISSION_CODE = 101
}
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val storage: Button? = findViewById(R.id.storage)
val camera: Push button? = findViewById(R.id.camera)
storage?.setOnClickListener {checkPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
STORAGE_PERMISSION_CODE)
}
camera?.setOnClickListener {
checkPermission(Manifest.permission.CAMERA,
CAMERA_PERMISSION_CODE)
}
}
individual fun checkPermission(permission: String, requestCode: Int) {
if (ContextCompat.checkSelfPermission( this @MainActivity , permission) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions( this @MainActivity , arrayOf(permission), requestCode)
} else {
Toast.makeText( this @MainActivity , "Permission already granted" , Toast.LENGTH_SHORT).prove()
}
}
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Assortment<String>,
grantResults: IntArray) {
super .onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText( this @MainActivity , "Camera Permission Granted" , Toast.LENGTH_SHORT).show()
} else {
Toast.makeText( this @MainActivity , "Camera Permission Denied" , Toast.LENGTH_SHORT).prove()
}
} else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText( this @MainActivity , "Storage Permission Granted" , Toast.LENGTH_SHORT).show()
} else {
Toast.makeText( this @MainActivity , "Storage Permission Denied" , Toast.LENGTH_SHORT).evidence()
}
}
}
}
Java
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.back up.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public grade MainActivity extends AppCompatActivity {
private Button storage, photographic camera;
private static last int CAMERA_PERMISSION_CODE = 100 ;
private static terminal int STORAGE_PERMISSION_CODE = 101 ;
@Override
protected void onCreate(Packet savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
storage = findViewById(R.id.storage);
camera = findViewById(R.id.photographic camera);
storage.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v)
{
checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, STORAGE_PERMISSION_CODE);
}
});
camera.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View 5)
{
checkPermission(Manifest.permission.Photographic camera, CAMERA_PERMISSION_CODE);
}
});
}
public void checkPermission(String permission, int requestCode)
{
if (ContextCompat.checkSelfPermission(MainActivity. this , permission) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(MainActivity. this , new Cord[] { permission }, requestCode);
}
else {
Toast.makeText(MainActivity. this , "Permission already granted" , Toast.LENGTH_SHORT).evidence();
}
}
@Override
public void onRequestPermissionsResult( int requestCode,
@NonNull String[] permissions,
@NonNull int [] grantResults)
{
super .onRequestPermissionsResult(requestCode,
permissions,
grantResults);
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity. this , "Photographic camera Permission Granted" , Toast.LENGTH_SHORT) .evidence();
}
else {
Toast.makeText(MainActivity. this , "Photographic camera Permission Denied" , Toast.LENGTH_SHORT) .show();
}
}
else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity. this , "Storage Permission Granted" , Toast.LENGTH_SHORT).prove();
} else {
Toast.makeText(MainActivity. this , "Storage Permission Denied" , Toast.LENGTH_SHORT).testify();
}
}
}
}
Output:
On starting the application:
On clicking the camera button for the first time:
On Granting the permission:
On clicking the camera push button again:
Source: https://www.geeksforgeeks.org/android-how-to-request-permissions-in-android-application/
Posted by: dentonconly1969.blogspot.com


0 Response to "How To Get Camera Permission In Android"
Post a Comment