code: private static void grantPermissions(Context ctx, Uri uri){
          Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          List<ResolveInfo> resolvedIntentActivities = ctx.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
          for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
              String packageName = resolvedIntentInfo.activityInfo.packageName;
  
              // Grant Permissions
              ctx.grantUriPermission(packageName, uri,
                      Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
                              Intent.FLAG_GRANT_READ_URI_PERMISSION);
          }
      }
docs: Grant URI permissions for all potential camera applications that can handle the capture
  intent.