I created an app where I have a place to put profile picture and it looks as follows:
When the camera button is click there are two options:
Now, I had like my app to ask the users if they allow me to grant camera permissions once they click on the "Camera" option but I can't figure out how to do it.
Currently, the app asks for permission at the first time the user enters this activity.
How can I make the grant permission to pop up only after this Cemra button is clicked?
My code is:
private void showPictureDialog(){
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems = {"Photo Gallery", "Camera" };
pictureDialog.setItems(pictureDialogItems,
(dialog, which) -> {
switch (which) {
case 0:
choosePhotoFromGallary();
break;
case 1:
takePhotoFromCamera();
break;
}
} );
pictureDialog.show();
}
public void choosePhotoFromGallary() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, AppConstants.GALLERY);
}
private void takePhotoFromCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, AppConstants.CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}
Bitmap fixBitmap;
if (requestCode == AppConstants.GALLERY) {
if (data != null) {
Uri contentURI = data.getData();
try {
DOES SOMETHING
} catch (IOException ignored) {
}
}
} else if (requestCode == AppConstants.CAMERA) {
DOES SOMETHING
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 5) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
}
}
Thank you