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.
activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == RESULT_OK && result.getData()!= null) {
Bundle data = result.getData().getExtras();
Uri myUri = (Uri) data.get("data");
profilePic.setImageURI(myUri);
}
});
uploadPicture.setOnClickListener(view -> {
Intent imagePickerIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imagePickerIntent.setType("image/*");
activityResultLauncher.launch(imagePickerIntent);
});
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
method to start Activity.
Then, override onRequestPermissionsResult() as follows:
Another, method as follows:
Another method for returning url/path for the image.