I'm having a trouble to request user permission based on what they've selected in chooser intent. For now, my program is request the permissions before the chooser intent show up. How can I achieve in a way that request the permissions after they've selected intent. The code below is request camera permission or storage permission first then only allow users to select they want to take picture from camera or gallery.
Dexter.checkPermissions(new MultiplePermissionsListener() {
Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<>();
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
List<PermissionGrantedResponse> list_GrantedPermission;
list_GrantedPermission = report.getGrantedPermissionResponses();
//check permission granted
if (report.areAllPermissionsGranted()){
intentList = addIntentsToList(mContext, intentList, takePhotoIntent);
intentList = addIntentsToList(mContext, intentList, pickIntent);
}
else if (list_GrantedPermission.size() > 0){
String grantedpermissionName = list_GrantedPermission.get(0).getPermissionName();
if (grantedpermissionName.equals(android.Manifest.permission.CAMERA))
intentList = addIntentsToList(mContext, intentList, takePhotoIntent);
else
intentList = addIntentsToList(mContext, intentList, pickIntent);
}
else {
}
//request a source
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
getString(R.string.message_select_source));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, REQUEST_CODE_PICTURE);
}
if (report.isAnyPermissionPermanentlyDenied()){
final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mContext);
alertBuilder.setTitle(getString(R.string.message_permission_required));
alertBuilder.setCancelable(true);
alertBuilder.setMessage(getString(R.string.message_permission_required));
alertBuilder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog dialog = alertBuilder.create();
dialog.show();
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}, android.Manifest.permission.CAMERA, android.Manifest.permission.READ_EXTERNAL_STORAGE);
}
});