How to Ask Runtime Permissions for Camera in Android , Runtime Storage Permissions

How to ask runtime permissions for Camera in Android , Runtime storage permissions

Below I have written a code for granting a runtime permissions for Camera, There is an Array of String in which you can give multiple requests granting as which is needed at runtime.

public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 200;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkPermission()) {
//main logic or main code

// . write your main code to execute, It will execute if the permission is already given.

} else {
requestPermission();
}
}

private boolean checkPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
return false;
}
return true;
}

private void requestPermission() {

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

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();

// main logic
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
showMessageOKCancel("You need to allow access permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermission();
}
}
});
}
}
}
break;
}
}

private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
}

How should I club android permission for camera and external storage

You can Add multiple permission into list.

Method for checking multiple Permission

public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 101;

public static boolean checkAndRequestPermissions(final Activity context) {
int ExtstorePermission = ContextCompat.checkSelfPermission(context,
Manifest.permission.READ_EXTERNAL_STORAGE);
int cameraPermission = ContextCompat.checkSelfPermission(context,
Manifest.permission.CAMERA);
List<String> listPermissionsNeeded = new ArrayList<>();
if (cameraPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.CAMERA);
}
if (WExtstorePermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded
.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(context, listPermissionsNeeded
.toArray(new String[listPermissionsNeeded.size()]),
REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}

Use it like,

if(checkAndRequestPermissions(MainActivity.this)){
doWork();
}

and handle PermissionsResult like,

@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
switch (requestCode) {
case Utility.REQUEST_ID_MULTIPLE_PERMISSIONS:
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),
"FlagUp Requires Access to Camara.", Toast.LENGTH_SHORT)
.show();
finish();
} else if (ContextCompat.checkSelfPermission(Splash_Activity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),
"FlagUp Requires Access to Your Storage.",
Toast.LENGTH_SHORT).show();
finish();
} else {
doWork();
}
break;
}
}

Happy Coding..

Read and Write external storage permission isn't working

in android API >= 23 you need to request permission at runtime. Take a look here

Something like this

        // Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);

// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}

however, Ted permission library is a gread lib to avoid such boilerplate code.

//call back after permission granted
PermissionListener permissionlistener = new PermissionListener() {
@Override
public void onPermissionGranted() {
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
}

@Override
public void onPermissionDenied(List<String> deniedPermissions) {
Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
}

};

//check all needed permissions together
TedPermission.with(this)
.setPermissionListener(permissionlistener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.check();

Request Permission after selected option(either camera or gallery) from intent chooser in android

Thanks to all for your support.

I solved it myself.

I appended below code in above method

Intent receiver = new Intent(MainActivity.this, IntentOptionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
//Create the Chooser
final Intent chooserIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
chooserIntent = Intent.createChooser(galleryIntent, "Select Source", pendingIntent.getIntentSender());
} else {
chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
}

Here is my broadcast receiver(IntentOptionReceiver) to notify which intent chooser option selected

public class IntentOptionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
for (String key : intent.getExtras().keySet()) {
Log.e("intentOptionReceiver", "Intent option clicked info" + intent.getExtras().get(key));
}
}
}

Do not forget to enter your broadcast receiver inside manifest.

How get permission for camera in android.(Specifically Marshmallow)

First check if the user has granted the permission:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED)

Then, you could use this to request to the user:

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

And in Marshmallow, it will appear in a dialog

How request permissions with Jetpack Compose?

Check out Google Accompanist's Jetpack Compose Permissions.

Bear in mind that, at the time of writing, the API is still considered experimental and will require the @ExperimentalPermissionsApi annotation when used.

  • Documentation and usage: https://google.github.io/accompanist/permissions/
  • Samples: https://github.com/google/accompanist/tree/main/sample/src/main/java/com/google/accompanist/sample/permissions


Related Topics



Leave a reply



Submit