I am trying to use the Activity Result APIs to handle the picking of a single photo for an app I am developing. I am trying to use one of the predefined contracts to keep things simple. So, I am attempting to use the ActivityResultContracts.PickVisualMedia() contract.
I am setting the Activity Result Launcher up as follows:
private ActivityResultLauncher<PickVisualMediaRequest> pickVisualMediaActivityResultLauncher;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
pickVisualMediaActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.PickVisualMedia(),
this::onPickVisualMediaActivityResult
);
}
And I am attempting to construct a PickVisualMediaRequest and launch the Activity Result Launcher here:
private void onSelectNewPhotoButtonClick() {
PickVisualMediaRequest request = new PickVisualMediaRequest.Builder()
.setMediaType(new ActivityResultContracts.PickVisualMedia.ImageOnly())
.build();
pickVisualMediaActivityResultLauncher.launch(request);
}
Issue is that Android Studio is complaining about ActivityResultContracts.PickVisualMedia.ImageOnly() not having proper visibility to be used, even though it is a valid VisualMediaType and the docs imply that it should be used this way:
I can't really find any code samples on this particular scenario. Am I missing something? Does the API have a visibility defect or am I just dumb today?
ActivityResultContracts.PickVisualMedia.ImageOnly
is a Kotlinobject
, not a class. In Kotlin, you would refer to that object simply asActivityResultContracts.PickVisualMedia.ImageOnly
. I forget how the JVM interop works for these. Regardless, you do not create an instance ofActivityResultContracts.PickVisualMedia.ImageOnly
, but use the pre-existing singleton instance. – DeduceActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE
. Writing it out this way in Java requires casting it toVisualMediaType
. After doing so in Java, the compiler complains about it being an incompatible cast. It's especially weird because it doesn't complain about it when written that way in Kotlin. I even decompiled that same working Kotlin code into Java and then weirdly the compiler complains about it. Very weird. – Acalia