Android has been on the edge of evolution for a while recently, with updates to androidx. It has deprecated startActivityForResult in favor of registerForActivityResult.
When starting an activity for a result, it is possible (and, in cases of memory-intensive operations such as camera usage, almost certain) that your process and your activity will be destroyed due to low memory.
For this reason, the Activity Result APIs decouple the result callback from the place in your code where you launch the other activity. As a result callback needs to be available when your process and activity are recreated, the callback must be unconditionally registered every time your activity is created, even if the logic of launching the other activity only happens based on user input or other business logic.
When in an Activity or a Fragment, the Activity Result APIs provide a registerForActivityResult() API for registering the result callback. registerForActivityResult() takes an ActivityResultContract and an ActivityResultCallback and returns an ActivityResultLauncher which you’ll use to launch the other activity.
An ActivityResultContract defines the input type needed to produce a result along with the output type of the result. The APIs provide default contracts for basic intent actions like taking a picture, requesting permissions, and so on. You can also create your own custom contracts.
ActivityResultCallback is a single method interface with an onActivityResult() method that takes an object of the output type defined in the ActivityResultContract:
Step 1: creating a global variable
public boolean GALLERY_INTENT_CALLED = false;
ActivityResultLauncher<Intent> activityResultLauncher;
ActivityResultLauncher<String> activityResultLauncherString;
Step 2 : Checking permission
public boolean checkPermissions(String permission){
Log.d(TAG, "checkPermissions: Checking permissions");
int permissionRequest= ActivityCompat.checkSelfPermission(RegistrationActivity.this,permission);
if(permissionRequest!= PackageManager.PERMISSION_GRANTED){
Log.d(TAG, "checkPermissions: \n permission was not granted for: "+permission);
return false;
}else{
Log.d(TAG, "checkPermissions: \n permission was granted for: "+permission);
return true;
}
}
Step 3: Verifying permission
public void verifyPermission(String[] permissions) {
Log.d(TAG, "verifyPermission: verifying permissions");
ActivityCompat.requestPermissions(RegistrationActivity.this,permissions,VERIFY_PERMISSIONS_REQUEST);
}
Step 4: opening Camera
if(checkPermissions(AppPermissions.CAMERA_PERMISSION[0])){
GALLERY_INTENT_CALLED = false;
Log.d(TAG, "onClick: starting camera");
Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activityResultLauncher.launch(cameraIntent);
}else{
verifyPermission(AppPermissions.PERMISSIONS);
}
Step 5: opening Gallery
if(checkPermissions(AppPermissions.READ_STORAGE_PERMISSION[0]) && checkPermissions((AppPermissions.WRITE_STORAGE_PERMISSION[0]))) {
GALLERY_INTENT_CALLED = true;
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
activityResultLauncher.launch(galleryIntent);
}else{
verifyPermission(AppPermissions.PERMISSIONS);
}
Step 6: Preview selected camera image
activityResultLauncher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), (ActivityResult result) -> {
if (result.getResultCode() == RESULT_OK && result.getData()!=null ) {
if (GALLERY_INTENT_CALLED) {
Uri resultData = result.getData().getData();
imgUserPhoto.setImageURI(resultData);
}else{
Bundle bundle = result.getData().getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
imgUserPhoto.setImageBitmap(bitmap);
}
}
});
Step 7: Preview selected gallery image
activityResultLauncherString=registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri result) {
imgUserPhoto.setImageURI(result);
}
});