Home > Enterprise >  How to upload and save an image from gallery. startActivityForResult deprecated
How to upload and save an image from gallery. startActivityForResult deprecated

Time:03-25

I'm currently learning how to use android studio in java and am trying to make a social media app. I am currently creating an edit profile page where the user would update their details and upload a profile picture.

I have been following tutorials online and all of the ones I have come across use the startActivityForResult method. It has been crossed out and wont call the method as it is deprecated. But I don't know what to use instead.

`ProfileImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //open gallery

            Intent OpenGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(OpenGalleryIntent, 1000);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1000){
        if(resultCode == Activity.RESULT_OK){
            Uri imageUri = data.getData();
            ProfileImage.setImageURI(imageUri);
            UploadImageToFirebase(imageUri);
        }
    }
}
private void UploadImageToFirebase(Uri image){
    StorageReference fileRef = storageReference.child("Profile.jpg");
    fileRef.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Toast.makeText(Edit_Profile.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
        }
    });![enter image description here](https://i.stack.imgur.com/padRc.jpg)`

I know there is an alternative but I don't understand how it works.

CodePudding user response:

Yes startActivityForeResult is deprecated.

Now you can use ActivityResultLauncher for the callbacks

https://developer.android.com/training/basics/intents/result#java

CodePudding user response:

startActivityForResult is indeed deprecated in later versions of AndroidX Activity and Fragment APIs (while I believe you can still use it despite of warning). New way to get result from activity is registerForActivityResult.

In your code you would need to create a launcher, which will handle result (selected image)

private final ActivityResultLauncher<Intent> launcher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK 
                       && result.getData() != null) {
                    Uri photoUri = result.getData().getData();
                    //use photoUri here
                }
            }
    );

and then launch this launcher in onClickListener

profileImage.setOnClickListener(view -> {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            launcher.launch(intent);
        });
  • Related