title: ActivityResultLauncher for uploading profile picture
body: <p>I want to pick a picture from the phone gallery to upload as the profile picture of the user in an app. And I want to get the URI for it so I can store it in the user database.</p>
  <pre><code>activityResultLauncher = registerForActivityResult(
              new ActivityResultContracts.StartActivityForResult(), result -&gt; {
                  if (result.getResultCode() == RESULT_OK &amp;&amp; result.getData()!= null) {
                      Bundle data = result.getData().getExtras();
                      Uri myUri = (Uri) data.get(&quot;data&quot;);
                      profilePic.setImageURI(myUri);
                  }
          });
  
      uploadPicture.setOnClickListener(view -&gt; {
          Intent imagePickerIntent = new Intent(Intent.ACTION_PICK, 
  MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          imagePickerIntent.setType(&quot;image/*&quot;);
          activityResultLauncher.launch(imagePickerIntent);
      });
  </code></pre>
  <p>Right now I can enter code here and open the gallery and browse through pictures but the app crashes when i select one and try to go back to my app from the gallery. Can anyone tell me how to fix my code? Thanks</p>