-1

I am trying to launch an activity after a user has selected a photo. I was trying to do this:

        uploadImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                selectImageIntent.setType("image/*");

                startActivityForResult(selectImageIntent, 1);

                Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
                goToActivityIntent.putExtra("email", email);
                goToActivityIntent.putExtra("donorEmail", donorEmail);
                goToActivityIntent.putExtra("orderId", orderId);
                goToActivityIntent.putExtra("uriString", uriString);

                view.getContext().startActivity(goToActivityIntent);
            }
        });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK && data != null) {
            uriString = data.getData().toString();
        }
    }

But I realised that with this code, the code for launching the activity (SendPhotoChangeActivity) executes before the user selects the image, crashing the app because the uriString variable is null.

I tried simply copy/pasting the code into onActivityResult(), but the view variable (in view.getContext()) was, of course, not recognized in onActivityResult().

I am thinking of simply replacing view.getContext() by getApplicationContext() in onActivityResult(). Is this the right thing to do? If not, please tell me how I can start an activity in onActivityResult().

Huzaifa
  • 482
  • 1
  • 7
  • 20

3 Answers3

1

If you are in Activity then you can just use this as Context

 Intent goToActivityIntent = new Intent(this, SendPhotoChangeActivity.class);

If you are in a Fragment then you can obtain Context by calling getContext()

Intent goToActivityIntent = new Intent(getContext(), SendPhotoChangeActivity.class);

And use that code inside onActivityResult() as you were trying to.

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • Thank you. Can you please tell me what is better: using ```this``` or using ```getApplicationContext()``` – Huzaifa Sep 11 '20 at 17:15
  • 1
    If you use getApplicationContext() here I think the app will crash refer this thread https://stackoverflow.com/questions/50117389/how-to-open-activity-using-getapplicationcontext and if you want to find out more about different contexts use this https://stackoverflow.com/questions/10347184/difference-and-when-to-use-getapplication-getapplicationcontext-getbasecon – Rainmaker Sep 11 '20 at 18:18
0

set an integer code for the act of selecting a picture like REQUEST_CODE_TAKE_PICTURE so you know that has happened, this works ok in Kotlin, I assume that works as well with java:

if (requestCode == REQUEST_CODE_TAKE_PICTURE && resultCode == Activity.RESULT_OK) {

  Intent goToActivityIntent = new Intent(view.getContext(),SendPhotoChangeActivity.class);
            goToActivityIntent.putExtra("email", email);
            goToActivityIntent.putExtra("donorEmail", donorEmail);
            goToActivityIntent.putExtra("orderId", orderId);
            goToActivityIntent.putExtra("uriString", uriString);

            view.getContext().startActivity(goToActivityIntent);
        if (data == null) {
            //Display an error
            println("error accuered at onActivityResult ")
            return
        }
narcis dpr
  • 939
  • 2
  • 12
  • 32
  • Sorry, but you did not address the problem. The problem is that view.getContext() cannot be called in onActivityResult() because view is an argument of the onClick() function in setOnClickListener(). – Huzaifa Sep 11 '20 at 16:34
0

Have you tried this simpler one:

uploadImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                selectImageIntent.setType("image/*");

                startActivityForResult(selectImageIntent, 1);
            }
        });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK && data != null) {
            uriString = data.getData().toString();
             Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
                goToActivityIntent.putExtra("email", email);
                goToActivityIntent.putExtra("donorEmail", donorEmail);
                goToActivityIntent.putExtra("orderId", orderId);
                goToActivityIntent.putExtra("uriString", uriString);

                startActivity(goToActivityIntent);
        }
    }

Just call

startActivity(goToActivityIntent);

to call the activity.

This assumes you are calling it from your activity or fragment. If this doesn't meet your requirements, let me know. There are other ways to implement this.

Basu
  • 763
  • 8
  • 27